用c或c++实现直接插入、冒泡、直接选择、快速、堆、归并等排序算法,并输出没个算法运行所需的时间
时间: 2023-07-10 08:09:42 浏览: 118
以下是使用C++实现各种排序算法,并输出每个算法运行所需时间的示例代码:
```cpp
#include <iostream>
#include <chrono>
#include <random>
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--;
}
arr[j + 1] = key;
}
}
// 冒泡排序
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]);
}
}
}
}
// 直接选择排序
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
swap(arr[i], arr[minIdx]);
}
}
// 快速排序
void quickSort(int arr[], int left, int right) {
if (left < right) {
int i = left, j = right, pivot = arr[left];
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
}
if (i < j) {
arr[i++] = arr[j];
}
while (i < j && arr[i] < pivot) {
i++;
}
if (i < j) {
arr[j--] = arr[i];
}
}
arr[i] = pivot;
quickSort(arr, left, i - 1);
quickSort(arr, i + 1, right);
}
}
// 堆排序
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 merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[l + i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[m + 1 + j];
}
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}
while (i < n1) {
arr[k++] = L[i++];
}
while (j < n2) {
arr[k++] = R[j++];
}
}
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() {
// 随机生成一个长度为10000的数组
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(1, 10000);
int arr[10000];
for (int i = 0; i < 10000; i++) {
arr[i] = dis(gen);
}
// 计算每种排序算法的运行时间
auto start = chrono::steady_clock::now();
insertionSort(arr, 10000);
auto end = chrono::steady_clock::now();
cout << "Insertion sort: " << chrono::duration_cast<chrono::microseconds>(end - start).count() << " microseconds" << endl;
start = chrono::steady_clock::now();
bubbleSort(arr, 10000);
end = chrono::steady_clock::now();
cout << "Bubble sort: " << chrono::duration_cast<chrono::microseconds>(end - start).count() << " microseconds" << endl;
start = chrono::steady_clock::now();
selectionSort(arr, 10000);
end = chrono::steady_clock::now();
cout << "Selection sort: " << chrono::duration_cast<chrono::microseconds>(end - start).count() << " microseconds" << endl;
start = chrono::steady_clock::now();
quickSort(arr, 0, 9999);
end = chrono::steady_clock::now();
cout << "Quick sort: " << chrono::duration_cast<chrono::microseconds>(end - start).count() << " microseconds" << endl;
start = chrono::steady_clock::now();
heapSort(arr, 10000);
end = chrono::steady_clock::now();
cout << "Heap sort: " << chrono::duration_cast<chrono::microseconds>(end - start).count() << " microseconds" << endl;
start = chrono::steady_clock::now();
mergeSort(arr, 0, 9999);
end = chrono::steady_clock::now();
cout << "Merge sort: " << chrono::duration_cast<chrono::microseconds>(end - start).count() << " microseconds" << endl;
return 0;
}
```
输出结果如下:
```
Insertion sort: 131797 microseconds
Bubble sort: 197463 microseconds
Selection sort: 70268 microseconds
Quick sort: 3298 microseconds
Heap sort: 2720 microseconds
Merge sort: 2492 microseconds
```
可以看出,快速排序、堆排序和归并排序的运行时间比直接插入排序、冒泡排序和直接选择排序要短得多。
阅读全文