나는이 정렬 알고리즘의 지속 시간을 계산하기 위해 작업 해왔다. 모든 정렬 방법을 2000 번 반복 한 다음 총 지속 시간을 2000으로 나누어 지속 시간에 적절한 값을 얻습니다. 문제는; 정렬 메소드의 특정 코드 부분이 취하는 정확한 시간 값을 표시하지 않습니다. 나는 duration
변수가 프로그램 흐름을 통해 값이 증가하는 것을 보여줍니다. 예를 들어, N = 10000
의 경우 insertionSort()
은 0.000635를, mergeSort()
은 0.00836을, heapSort()
은 0.018485를 얻습니다.이 순서를 변경하면 duration
도 알고리즘 유형에 관계없이 프로그램을 통해 계속 올라갑니다. 나는 각 프로세스마다 다른 지속 시간 값을 주려고했지만 작동하지 않았다. 다른 사람이이 문제를 이해하도록 도와 줄 수 있습니까? 아니면 스타일을 측정하는 다른 시간이 있습니까?C++ 정렬 알고리즘 기간
죄송합니다. 이것은 바보 같은 문제이고 내 나쁜 문법 때문입니다.
int main(){
srand(time(NULL));
int N, duration;
cout << endl << "N : ";
cin >> N; // N is array sze.
cout << endl;
// a4 would be the buffer array (for calculating proper duration).
int *a1 = new int[N];
int *a2 = new int[N];
int *a3 = new int[N];
int *a4 = new int[N];
cout << endl << "Unsorted array : " << endl;
for (int i = 0; i < N; i++){
a4[i] = rand() % 100;
cout << a4[i] << " ";
}
/*------------------------------------------------------------------------------*/
cout << endl << endl <<"Sorting with Insertion Sort, please wait..." << endl;
for(int i = 0; i < 2000; i++){
a1 = a4;
duration = clock();
insertionSort(a1, N - 1);
duration += clock() - duration;
}
cout << endl << "Insertion sort : " << endl;
print(a1, N);
cout << endl << endl << "Approximate duration for Insertion Sort : ";
cout << (double) (duration/2000)/CLOCKS_PER_SEC;
cout << " s." << endl;
/*------------------------------------------------------------------------------*/
cout << endl << endl << "Sorting with Merge Sort, please wait..." << endl;
for(int i = 0; i < 2000; i++){
a2 = a4;
duration = clock();
mergeSort(a2, 0, N - 1);
duration += clock() - duration;
}
cout << endl << "Merge sort : " << endl;
print(a2, N);
cout << endl << endl << "Approximate duration for Merge Sort : ";
cout << (double) (duration/2000)/CLOCKS_PER_SEC;
cout << " s."<< endl << endl;
/*------------------------------------------------------------------------------*/
cout << endl << endl << "Sorting with Heap Sort, please wait..." << endl;
for(int i = 0; i < 2000; i++){
a3 = a4;
duration = clock();
heapSort(a3, N);
duration += clock() - duration;
}
cout << endl << "Heap sort : " << endl;
print(a3, N);
cout << endl << endl << "Approximate duration for Heap Sort : ";
cout << (double) (duration/2000)/CLOCKS_PER_SEC;
cout << " s."<< endl << endl;
return 0;
}
원본 데이터의 순서가 정렬 성능에도 영향을줍니다. 한 세트의 데이터에서 여러 번 정렬을 수행해야 할뿐만 아니라보다 정확하거나 전체적인 성능 등급을 얻으려면 많은 데이터 세트에 대한 정렬을 수행해야합니다. 또한 타이밍은 컴퓨터에서 실행중인 다른 응용 프로그램의 영향을받을 수 있습니다. –