用c++写出测试以下排算法的时间性能。 直接插入排序希尔排序冒泡排序快速排序直接选择排序堆排序
时间: 2023-07-05 17:08:20 浏览: 118
以下是使用 C++ 编写的测试代码,用于比较不同排序算法的时间性能:
```cpp
#include <iostream>
#include <chrono>
#include <algorithm>
using namespace std;
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; ++i) {
int key = arr[i];
int 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) {
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) {
bool swapped = false;
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
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++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
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 minIndex = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
}
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
swap(arr[i], arr[largest]);
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) {
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 << endl;
}
int main() {
int n = 10000;
int arr1[n], arr2[n], arr3[n], arr4[n], arr5[n], arr6[n];
for (int i = 0; i < n; ++i) {
arr1[i] = rand() % 10000;
arr2[i] = arr1[i];
arr3[i] = arr1[i];
arr4[i] = arr1[i];
arr5[i] = arr1[i];
arr6[i] = arr1[i];
}
auto start = chrono::high_resolution_clock::now();
insertionSort(arr1, n);
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Time taken by Insertion Sort: " << duration.count() << " microseconds" << endl;
start = chrono::high_resolution_clock::now();
shellSort(arr2, n);
end = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Time taken by Shell Sort: " << duration.count() << " microseconds" << endl;
start = chrono::high_resolution_clock::now();
bubbleSort(arr3, n);
end = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Time taken by Bubble Sort: " << duration.count() << " microseconds" << endl;
start = chrono::high_resolution_clock::now();
quickSort(arr4, 0, n - 1);
end = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Time taken by Quick Sort: " << duration.count() << " microseconds" << endl;
start = chrono::high_resolution_clock::now();
selectionSort(arr5, n);
end = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Time taken by Selection Sort: " << duration.count() << " microseconds" << endl;
start = chrono::high_resolution_clock::now();
heapSort(arr6, n);
end = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Time taken by Heap Sort: " << duration.count() << " microseconds" << endl;
return 0;
}
```
注意,此代码的测试结果会受到随机数生成器的影响,因此每次运行结果可能不同。
阅读全文