⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 averagesorttime.cpp

📁 这是数据结构、算法与应用-C++语言描述的代码
💻 CPP
字号:
// average sort times 

#include <iostream>
#include <time.h>
#include <math.h>
#include "insertionSort.h"
#include "heapSort.h"
#include "mergeSort.h"
#include "quickSort.h"

using namespace std;

void main(void)
{
   double clocksPerMillis = double(CLOCKS_PER_SEC) / 1000;
                      // clock ticks per millisecond
   double clocksPerSec = double(CLOCKS_PER_SEC);
   int theN [] = {0, 50, 100, 200, 300, 400, 500, 1000, 2000, 3000, 4000, 5000};
   int numPoints = 12;
   int step = 10;

   cout << "The average-case times, in milliseconds, are" << endl;
   cout << "n \trepetitions \ttime/sort" << endl;
   for (int i = 0; i < numPoints; i++)
   {
      int n = theN[i];
      // create element arrays
      int *a = new int [n + 1];

      long startTime = clock();
      long counter = 0;
      while (counter < 200 ||
             clock( ) - startTime < clocksPerSec)
      {
         counter++;
         // create random data
         // for (int i = 0; i < n; i++)
         for (int i = 1; i <= n; i++)
            a[i] = rand();

         // sort the elements
         // mergeSort(a, n);
         // quickSort(a,n);
         // insertionSort(a,n);
          heapSort(a,n);
      }

      double elapsedTime = ((double) (clock() - startTime)) / counter;
      elapsedTime /= clocksPerMillis;
      cout << n << "\t" << counter << "\t\t" 
           << elapsedTime << endl;
   }
}

⌨️ 快捷键说明

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