测试以下排序算法的时间性能 直接插入排序 希尔排序 冒泡排序 快速排序 直接选择排序 堆排序 c++
时间: 2023-07-07 18:46:29 浏览: 126
C和C++排序算法
好的,我可以给你一个使用 C++ 实现的测试代码,包括直接插入排序、希尔排序、冒泡排序、快速排序、直接选择排序和堆排序,代码如下:
```cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
// 直接插入排序
void insertSort(int arr[], int len) {
for (int i = 1; i < len; i++) {
int temp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
// 希尔排序
void shellSort(int arr[], int len) {
int gap = len / 2;
while (gap > 0) {
for (int i = gap; i < len; i++) {
int temp = arr[i];
int j = i - gap;
while (j >= 0 && arr[j] > temp) {
arr[j + gap] = arr[j];
j -= gap;
}
arr[j + gap] = temp;
}
gap /= 2;
}
}
// 冒泡排序
void bubbleSort(int arr[], int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; 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 left, int right) {
int pivot = arr[left];
while (left < right) {
while (left < right && arr[right] >= pivot) right--;
arr[left] = arr[right];
while (left < right && arr[left] <= pivot) left++;
arr[right] = arr[left];
}
arr[left] = pivot;
return left;
}
void quickSort(int arr[], int left, int right) {
if (left < right) {
int mid = partition(arr, left, right);
quickSort(arr, left, mid - 1);
quickSort(arr, mid + 1, right);
}
}
// 直接选择排序
void selectSort(int arr[], int len) {
for (int i = 0; i < len - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
// 堆排序
void maxHeapify(int arr[], int start, int end) {
int dad = start;
int son = dad * 2 + 1;
while (son <= end) {
if (son + 1 <= end && arr[son] < arr[son + 1]) {
son++;
}
if (arr[dad] > arr[son]) {
return;
} else {
int temp = arr[dad];
arr[dad] = arr[son];
arr[son] = temp;
dad = son;
son = dad * 2 + 1;
}
}
}
void heapSort(int arr[], int len) {
for (int i = len / 2 - 1; i >= 0; i--) {
maxHeapify(arr, i, len - 1);
}
for (int i = len - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
maxHeapify(arr, 0, i - 1);
}
}
int main() {
srand(time(0)); // 用当前时间作为随机数种子
const int N = 10000;
int arr[N];
for (int i = 0; i < N; i++) {
arr[i] = rand(); // 生成随机数
}
clock_t start, end;
double duration;
start = clock();
insertSort(arr, N);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
cout << "直接插入排序用时:" << duration << "秒" << endl;
start = clock();
shellSort(arr, N);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
cout << "希尔排序用时:" << duration << "秒" << endl;
start = clock();
bubbleSort(arr, N);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
cout << "冒泡排序用时:" << duration << "秒" << endl;
start = clock();
quickSort(arr, 0, N - 1);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
cout << "快速排序用时:" << duration << "秒" << endl;
start = clock();
selectSort(arr, N);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
cout << "直接选择排序用时:" << duration << "秒" << endl;
start = clock();
heapSort(arr, N);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
cout << "堆排序用时:" << duration << "秒" << endl;
return 0;
}
```
这个代码会生成一个长度为 10000 的随机数数组,并分别对其进行直接插入排序、希尔排序、冒泡排序、快速排序、直接选择排序和堆排序,并输出每个排序算法所用的时间。你可以根据需要修改数据规模和排序算法。
阅读全文