time2.cpp

来自「data structures, algorithms and Applicat」· C++ 代码 · 共 29 行

CPP
29
字号
// worst case insertion sort times
// correct way

#include <iostream.h>
#include <time.h>
#include "insort.h"

void main(void)
{
   int a[1000], n, i, step = 10;
   long counter;
   float seconds;
   clock_t start, finish;
   for (n = 0; n <= 1000; n += step) {
      // get time for size n
      start = clock( ); counter = 0;
      while (clock( ) - start < 10) {
         counter++;
         for (i = 0; i < n; i++)
            a[i] = n - i; // initialize
         InsertionSort(a, n);
         }
      finish = clock( );
      seconds = (finish - start) / CLK_TCK;
      cout << n << ' ' << counter << ' ' << seconds
           << ' ' << seconds / counter << endl;
      if (n == 100) step = 100;}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?