实现合并排序算法。要排序的整型数组元素可以用随机数生成器生成,运行程序,分别记录对十万,一百万,一千万,五千万个数据排序所用的时间,列出表格显示统计出来的时间。并单独显示一个100个数据的排序结果。使用c++编写
时间: 2024-03-25 17:38:01 浏览: 161
编程实现希尔、快速、堆、归并排序算法,随机产生10000个随机数存入磁盘文件,读取,并将不同排序方法的结果存入文件中(含报告)
5星 · 资源好评率100%
好的,以下是用 C++ 实现的合并排序算法程序,满足您的要求:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
using namespace std;
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int i, n;
double time_used;
// 10^5
n = 100000;
int arr1[n];
for (i = 0; i < n; i++)
arr1[i] = rand();
auto start1 = chrono::high_resolution_clock::now();
mergeSort(arr1, 0, n - 1);
auto end1 = chrono::high_resolution_clock::now();
time_used = chrono::duration_cast<chrono::microseconds>(end1 - start1).count() / 1e6;
cout << "Time taken for " << n << " elements: " << time_used << " seconds" << endl;
// 10^6
n = 1000000;
int arr2[n];
for (i = 0; i < n; i++)
arr2[i] = rand();
auto start2 = chrono::high_resolution_clock::now();
mergeSort(arr2, 0, n - 1);
auto end2 = chrono::high_resolution_clock::now();
time_used = chrono::duration_cast<chrono::microseconds>(end2 - start2).count() / 1e6;
cout << "Time taken for " << n << " elements: " << time_used << " seconds" << endl;
// 10^7
n = 10000000;
int arr3[n];
for (i = 0; i < n; i++)
arr3[i] = rand();
auto start3 = chrono::high_resolution_clock::now();
mergeSort(arr3, 0, n - 1);
auto end3 = chrono::high_resolution_clock::now();
time_used = chrono::duration_cast<chrono::microseconds>(end3 - start3).count() / 1e6;
cout << "Time taken for " << n << " elements: " << time_used << " seconds" << endl;
// 5*10^7
n = 50000000;
int arr4[n];
for (i = 0; i < n; i++)
arr4[i] = rand();
auto start4 = chrono::high_resolution_clock::now();
mergeSort(arr4, 0, n - 1);
auto end4 = chrono::high_resolution_clock::now();
time_used = chrono::duration_cast<chrono::microseconds>(end4 - start4).count() / 1e6;
cout << "Time taken for " << n << " elements: " << time_used << " seconds" << endl;
// print sorted array of 100 elements
n = 100;
int arr5[n];
for (i = 0; i < n; i++)
arr5[i] = rand();
cout << "Before sorting:" << endl;
printArray(arr5, n);
mergeSort(arr5, 0, n - 1);
cout << "After sorting:" << endl;
printArray(arr5, n);
return 0;
}
```
这个程序与之前的 C 语言程序相似,但使用了 C++ 的库函数 `chrono` 来计时。它同样通过 `mergeSort` 函数实现了合并排序算法,并输出了对于四个不同规模的数组所用的时间。最后,它还输出了一个由随机生成的100个元素组成的数组的排序结果。
注意:这个程序只是演示了如何使用合并排序算法,实际应用中可能需要考虑更多因素,如算法的稳定性、内存占用等。
阅读全文