c++实现1.定义待排序表的长度为n。 2.初始化随机数发生器,产生n个随机数构成的顺序表。 3.创建输出排序表函数,取得系统时间函数,直接插入排序函数,冒泡排序函数,快速排序函数,直接选择排序函数,筛选函数,对排序函数,归并排序函数。 4.主程序调用各个排序函数,输出各排序算法对长度为n的顺序表进行排序所耗费的时间
时间: 2024-02-05 18:13:09 浏览: 91
下面是一份使用C++实现各种排序算法并测试时间的示例代码:
```cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
using namespace std;
using namespace std::chrono;
// 直接插入排序
void insertSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int j = i;
int tmp = arr[j];
while (j > 0 && tmp < arr[j-1]) {
arr[j] = arr[j-1];
j--;
}
arr[j] = tmp;
}
}
// 冒泡排序
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-1-i; j++) {
if (arr[j] > arr[j+1]) {
swap(arr[j], arr[j+1]);
}
}
}
}
// 快速排序
void quickSort(int arr[], int left, int right) {
if (left >= right) return;
int i = left, j = right, pivot = arr[left];
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;
quickSort(arr, left, i-1);
quickSort(arr, i+1, right);
}
// 直接选择排序
void selectSort(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 siftDown(int arr[], int i, int n) {
int tmp = arr[i];
while (2*i + 1 < n) {
int j = 2*i + 1;
if (j+1 < n && arr[j+1] > arr[j]) j++;
if (tmp >= arr[j]) break;
arr[i] = arr[j];
i = j;
}
arr[i] = tmp;
}
void heapSort(int arr[], int n) {
for (int i = n/2-1; i >= 0; i--) {
siftDown(arr, i, n);
}
for (int i = n-1; i > 0; i--) {
swap(arr[0], arr[i]);
siftDown(arr, 0, i);
}
}
// 归并排序
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int* L = new int[n1];
int* R = new int[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[left+i];
}
for (int i = 0; i < n2; i++) {
R[i] = arr[mid+1+i];
}
int i = 0, j = 0, k = left;
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++];
}
delete[] L;
delete[] R;
}
void mergeSort(int arr[], int left, int right) {
if (left >= right) return;
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid+1, right);
merge(arr, left, mid, right);
}
// 输出数组
void printArr(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
srand(time(NULL));
int n;
cout << "请输入待排序表的长度:";
cin >> n;
int* arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = rand() % 100;
}
auto start = high_resolution_clock::now();
insertSort(arr, n);
auto stop = high_resolution_clock::now();
cout << "直接插入排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl;
start = high_resolution_clock::now();
bubbleSort(arr, n);
stop = high_resolution_clock::now();
cout << "冒泡排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl;
start = high_resolution_clock::now();
quickSort(arr, 0, n-1);
stop = high_resolution_clock::now();
cout << "快速排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl;
start = high_resolution_clock::now();
selectSort(arr, n);
stop = high_resolution_clock::now();
cout << "直接选择排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl;
start = high_resolution_clock::now();
heapSort(arr, n);
stop = high_resolution_clock::now();
cout << "筛选排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl;
start = high_resolution_clock::now();
mergeSort(arr, 0, n-1);
stop = high_resolution_clock::now();
cout << "归并排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl;
delete[] arr;
return 0;
}
```
这份代码实现了6种排序算法,包括直接插入排序、冒泡排序、快速排序、直接选择排序、筛选排序、归并排序。在主程序中,我们先生成一个长度为n的随机数组,然后依次调用各个排序函数,计算每个函数的运行时间,并输出到屏幕上。
阅读全文