用C++写一段代码 定义待排序表的长度为n。输入n 初始化随机数发生器,产生n个随机数构成的顺序表。 创建输出排序表函数,取得系统时间函数,直接插入排序函数,冒泡排序函数,快速排序函数,直接选择排序函数,筛选函数,对排序函数,归并排序函数。 主程序调用各个排序函数,输出各排序算法对长度为n的顺序表进行排序所耗费的时间为多少毫秒。
时间: 2024-02-06 15:12:34 浏览: 62
以下是您要求的代码,其中包括了各种排序算法和输出排序表的函数:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
using namespace std;
// 直接插入排序
void insertion_sort(int arr[], int n) {
for (int i = 1; i < n; ++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 bubble_sort(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;
}
}
}
}
// 快速排序
void quick_sort(int arr[], int left, int right) {
if (left >= right) {
return;
}
int pivot = arr[left];
int i = left, j = right;
while (i < j) {
while (i < j && arr[j] >= pivot) {
--j;
}
arr[i] = arr[j];
while (i < j && arr[i] <= pivot) {
++i;
}
arr[j] = arr[i];
}
arr[i] = pivot;
quick_sort(arr, left, i - 1);
quick_sort(arr, i + 1, right);
}
// 直接选择排序
void selection_sort(int arr[], int n) {
for (int i = 0; i < n - 1; ++i) {
int k = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[k]) {
k = j;
}
}
if (k != i) {
int temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
}
// 筛选函数
void sift(int arr[], int i, int j) {
int temp = arr[i];
int k = i * 2 + 1;
while (k <= j) {
if (k < j && arr[k] < arr[k + 1]) {
++k;
}
if (arr[k] > temp) {
arr[i] = arr[k];
i = k;
k = i * 2 + 1;
} else {
break;
}
}
arr[i] = temp;
}
// 堆排序
void heap_sort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; --i) {
sift(arr, i, n - 1);
}
for (int i = n - 1; i > 0; --i) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
sift(arr, 0, i - 1);
}
}
// 归并排序
void merge(int arr[], int left, int mid, int right) {
int* temp = new int[right - left + 1];
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (i = 0; i < k; ++i) {
arr[left + i] = temp[i];
}
delete[] temp;
}
void merge_sort(int arr[], int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
// 输出排序表
void print_array(int arr[], int n) {
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int n;
cout << "请输入待排序表的长度n:";
cin >> n;
int* arr = new int[n];
srand(time(nullptr));
for (int i = 0; i < n; ++i) {
arr[i] = rand() % 100;
}
cout << "原始待排序表为:";
print_array(arr, n);
auto start_time = chrono::high_resolution_clock::now();
insertion_sort(arr, n);
auto end_time = chrono::high_resolution_clock::now();
cout << "直接插入排序结果为:";
print_array(arr, n);
cout << "直接插入排序耗时:" << chrono::duration_cast<chrono::microseconds>(end_time - start_time).count() << " 微秒" << endl;
start_time = chrono::high_resolution_clock::now();
bubble_sort(arr, n);
end_time = chrono::high_resolution_clock::now();
cout << "冒泡排序结果为:";
print_array(arr, n);
cout << "冒泡排序耗时:" << chrono::duration_cast<chrono::microseconds>(end_time - start_time).count() << " 微秒" << endl;
start_time = chrono::high_resolution_clock::now();
quick_sort(arr, 0, n - 1);
end_time = chrono::high_resolution_clock::now();
cout << "快速排序结果为:";
print_array(arr, n);
cout << "快速排序耗时:" << chrono::duration_cast<chrono::microseconds>(end_time - start_time).count() << " 微秒" << endl;
start_time = chrono::high_resolution_clock::now();
selection_sort(arr, n);
end_time = chrono::high_resolution_clock::now();
cout << "直接选择排序结果为:";
print_array(arr, n);
cout << "直接选择排序耗时:" << chrono::duration_cast<chrono::microseconds>(end_time - start_time).count() << " 微秒" << endl;
start_time = chrono::high_resolution_clock::now();
heap_sort(arr, n);
end_time = chrono::high_resolution_clock::now();
cout << "堆排序结果为:";
print_array(arr, n);
cout << "堆排序耗时:" << chrono::duration_cast<chrono::microseconds>(end_time - start_time).count() << " 微秒" << endl;
start_time = chrono::high_resolution_clock::now();
merge_sort(arr, 0, n - 1);
end_time = chrono::high_resolution_clock::now();
cout << "归并排序结果为:";
print_array(arr, n);
cout << "归并排序耗时:" << chrono::duration_cast<chrono::microseconds>(end_time - start_time).count() << " 微秒" << endl;
delete[] arr;
return 0;
}
```
在上述代码中,我们采用了 `chrono` 库来测量各个排序算法的耗时,输出的单位是微秒。您可以根据需要修改输出的单位。
阅读全文