问题描述:对 30000 个随机整数,利用插入排序、希尔排序、起泡排序、快速排序、选择排序、堆排序、归并排序等排序方法进行排序,并统计每一种排序上机所花费的时间。*输入的数据形式为任何一个正整数,大小不限。*输出的形式:数字大小逐个递增的数列C++实现
时间: 2024-05-01 18:23:29 浏览: 90
以下是一个简单的C++程序,它可以对输入的30000个随机整数进行排序,并统计每个排序算法的运行时间。其中包括插入排序、希尔排序、冒泡排序、快速排序、选择排序、堆排序和归并排序。
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
using namespace std;
using namespace std::chrono;
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void shellSort(int arr[], int n) {
for (int gap = n/2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i += 1) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high- 1; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void selectionSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
int min_idx = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n && arr[l] > arr[largest]) {
largest = l;
}
if (r < n && arr[r] > arr[largest]) {
largest = r;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n/2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n-1; i >= 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
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);
}
}
int main() {
const int n = 30000;
int arr1[n], arr2[n], arr3[n], arr4[n], arr5[n], arr6[n], arr7[n];
srand(time(0));
for (int i = 0; i < n; i++) {
int num = rand();
arr1[i] = num;
arr2[i] = num;
arr3[i] = num;
arr4[i] = num;
arr5[i] = num;
arr6[i] = num;
arr7[i] = num;
}
auto start = high_resolution_clock::now();
insertionSort(arr1, n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Insertion sort: " << duration.count() << "ms" << endl;
start = high_resolution_clock::now();
shellSort(arr2, n);
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
cout << "Shell sort: " << duration.count() << "ms" << endl;
start = high_resolution_clock::now();
bubbleSort(arr3, n);
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
cout << "Bubble sort: " << duration.count() << "ms" << endl;
start = high_resolution_clock::now();
quickSort(arr4, 0, n-1);
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
cout << "Quick sort: " << duration.count() << "ms" << endl;
start = high_resolution_clock::now();
selectionSort(arr5, n);
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
cout << "Selection sort: " << duration.count() << "ms" << endl;
start = high_resolution_clock::now();
heapSort(arr6, n);
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
cout << "Heap sort: " << duration.count() << "ms" << endl;
start = high_resolution_clock::now();
mergeSort(arr7, 0, n-1);
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
cout << "Merge sort: " << duration.count() << "ms" << endl;
return 0;
}
```
注意:以上代码中,使用了C++11的chrono库,它可以用来测量程序的运行时间。如果你使用的是旧版C++,则需要使用其他方式来测量运行时间。
阅读全文