问题描述
这个程序能很好地测量时间吗?因为我有非常奇怪的结果,当“n”有 1 000 000 个元素时,时间是 0.06,但我确定它太快了。问题出在哪儿?在 QSort (pivot - 3 的中位数和元素 = rand()%100) 我有 97810 毫秒......这是什么原因?代码中是否有遗漏或错误?
#include <iostream>
#include <ctime>
using namespace std;
const int n = 100000;
void heapify(int arr[],int n,int root)
{
int largest = root;
const int left = 2 * root + 1;
const int right = left + 1;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != root) {
swap(arr[root],arr[largest]);
heapify(arr,n,largest);
}
}
void heapSort(int arr[],int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr,i);
for (int i = n - 1; i >= 0; i--) {
swap(arr[0],arr[i]);
heapify(arr,i,0);
}
}
void printArray(int arr[],int n)
{
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << "\n";
}
int main()
{
int *arr = new int[n];
for(int i = 0; i< n; i++)
arr[i] = rand()%100;
// cout << "Not-Sorted array is "<<endl<<endl;
// printArray(arr,n);
cout<<endl;
const auto start = clock();
heapSort(arr,n);
const auto stop = clock();
const auto time = static_cast<double>(stop - start) / CLOCKS_PER_SEC;
// cout << "Sorted array is "<<endl<<endl;
// printArray(arr,n);
cout<<endl<< "Time is : "<<time<<endl<<endl;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)