用c++实现采用几组不同数据测试以下排算法的时间性能。 直接插入排序希尔排序冒泡排序快速排序直接选择排序堆排序
时间: 2023-07-07 17:08:07 浏览: 115
好的,我可以给你提供一个示例代码,用于测试这几种排序算法的时间性能。以下是使用 C++ 实现各种排序算法的示例代码:
```c++
#include <iostream>
#include <chrono>
#include <random>
#include <algorithm>
using namespace std;
void directInsertionSort(int arr[], int n) {
int i, j, key;
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]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
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) {
int i, j, min_idx;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
swap(arr[min_idx], arr[i]);
}
}
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) {
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);
}
}
int main() {
const int N = 10000;
int arr[N];
mt19937 mt_rand(time(0));
uniform_int_distribution<int> distribution(0, 1000);
for (int i = 0; i < N; i++) {
arr[i] = distribution(mt_rand);
}
auto start = chrono::high_resolution_clock::now();
directInsertionSort(arr, N);
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Direct Insertion Sort time: " << duration.count() << " microseconds" << endl;
random_shuffle(arr, arr + N); // shuffle the array
start = chrono::high_resolution_clock::now();
shellSort(arr, N);
end = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Shell Sort time: " << duration.count() << " microseconds" << endl;
random_shuffle(arr, arr + N); // shuffle the array
start = chrono::high_resolution_clock::now();
bubbleSort(arr, N);
end = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Bubble Sort time: " << duration.count() << " microseconds" << endl;
random_shuffle(arr, arr + N); // shuffle the array
start = chrono::high_resolution_clock::now();
quickSort(arr, 0, N - 1);
end = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Quick Sort time: " << duration.count() << " microseconds" << endl;
random_shuffle(arr, arr + N); // shuffle the array
start = chrono::high_resolution_clock::now();
selectionSort(arr, N);
end = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Selection Sort time: " << duration.count() << " microseconds" << endl;
random_shuffle(arr, arr + N); // shuffle the array
start = chrono::high_resolution_clock::now();
heapSort(arr, N);
end = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(end - start);
cout << "Heap Sort time: " << duration.count() << " microseconds" << endl;
return 0;
}
```
这个代码使用 C++ 实现了直接插入排序、希尔排序、冒泡排序、快速排序、直接选择排序和堆排序,并且使用随机数生成器生成了一个长度为 10000 的数组来测试每种排序算法的时间性能。输出结果将给出每种排序算法的时间(以微秒为单位)。
你可以使用这个代码作为模板,修改输入数据和排序算法,来测试任何其他排序算法的时间性能。
阅读全文