用c++采用几组不同数据测试以下排算法的时间性能。 直接插入排序希尔排序冒泡排序快速排序直接选择排序堆排序的代码
时间: 2023-07-18 10:05:52 浏览: 91
下面是直接插入排序、希尔排序、冒泡排序、快速排序、直接选择排序、堆排序的 C++ 代码:
```c++
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <algorithm>
using namespace std;
using namespace std::chrono;
// 直接插入排序
void insertion_sort(vector<int>& nums) {
for (int i = 1; i < nums.size(); ++i) {
int j = i - 1;
int key = nums[i];
while (j >= 0 && nums[j] > key) {
nums[j+1] = nums[j];
--j;
}
nums[j+1] = key;
}
}
// 希尔排序
void shell_sort(vector<int>& nums) {
int n = nums.size();
for (int gap = n/2; gap > 0; gap /= 2) {
for (int i = gap; i < n; ++i) {
int temp = nums[i];
int j = i;
while (j >= gap && nums[j-gap] > temp) {
nums[j] = nums[j-gap];
j -= gap;
}
nums[j] = temp;
}
}
}
// 冒泡排序
void bubble_sort(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n-1; ++i) {
for (int j = 0; j < n-i-1; ++j) {
if (nums[j] > nums[j+1]) {
swap(nums[j], nums[j+1]);
}
}
}
}
// 快速排序
void quick_sort(vector<int>& nums, int left, int right) {
if (left >= right) return;
int pivot = nums[left];
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= pivot) --j;
nums[i] = nums[j];
while (i < j && nums[i] <= pivot) ++i;
nums[j] = nums[i];
}
nums[i] = pivot;
quick_sort(nums, left, i-1);
quick_sort(nums, i+1, right);
}
// 直接选择排序
void selection_sort(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n; ++i) {
int min_idx = i;
for (int j = i+1; j < n; ++j) {
if (nums[j] < nums[min_idx]) {
min_idx = j;
}
}
swap(nums[i], nums[min_idx]);
}
}
// 堆排序
void heapify(vector<int>& nums, int n, int i) {
int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
if (left < n && nums[left] > nums[largest]) {
largest = left;
}
if (right < n && nums[right] > nums[largest]) {
largest = right;
}
if (largest != i) {
swap(nums[i], nums[largest]);
heapify(nums, n, largest);
}
}
void heap_sort(vector<int>& nums) {
int n = nums.size();
for (int i = n/2-1; i >= 0; --i) {
heapify(nums, n, i);
}
for (int i = n-1; i > 0; --i) {
swap(nums[0], nums[i]);
heapify(nums, i, 0);
}
}
int main() {
// 生成随机数
const int N = 10000;
vector<int> nums(N);
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(-1000, 1000);
for (int i = 0; i < N; ++i) {
nums[i] = dis(gen);
}
// 测试直接插入排序
auto start_time = high_resolution_clock::now();
insertion_sort(nums);
auto end_time = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(end_time - start_time);
cout << "直接插入排序耗时: " << duration.count() << " 微秒" << endl;
// 重新生成随机数
for (int i = 0; i < N; ++i) {
nums[i] = dis(gen);
}
// 测试希尔排序
start_time = high_resolution_clock::now();
shell_sort(nums);
end_time = high_resolution_clock::now();
duration = duration_cast<microseconds>(end_time - start_time);
cout << "希尔排序耗时: " << duration.count() << " 微秒" << endl;
// 重新生成随机数
for (int i = 0; i < N; ++i) {
nums[i] = dis(gen);
}
// 测试冒泡排序
start_time = high_resolution_clock::now();
bubble_sort(nums);
end_time = high_resolution_clock::now();
duration = duration_cast<microseconds>(end_time - start_time);
cout << "冒泡排序耗时: " << duration.count() << " 微秒" << endl;
// 重新生成随机数
for (int i = 0; i < N; ++i) {
nums[i] = dis(gen);
}
// 测试快速排序
start_time = high_resolution_clock::now();
quick_sort(nums, 0, N-1);
end_time = high_resolution_clock::now();
duration = duration_cast<microseconds>(end_time - start_time);
cout << "快速排序耗时: " << duration.count() << " 微秒" << endl;
// 重新生成随机数
for (int i = 0; i < N; ++i) {
nums[i] = dis(gen);
}
// 测试直接选择排序
start_time = high_resolution_clock::now();
selection_sort(nums);
end_time = high_resolution_clock::now();
duration = duration_cast<microseconds>(end_time - start_time);
cout << "直接选择排序耗时: " << duration.count() << " 微秒" << endl;
// 重新生成随机数
for (int i = 0; i < N; ++i) {
nums[i] = dis(gen);
}
// 测试堆排序
start_time = high_resolution_clock::now();
heap_sort(nums);
end_time = high_resolution_clock::now();
duration = duration_cast<microseconds>(end_time - start_time);
cout << "堆排序耗时: " << duration.count() << " 微秒" << endl;
return 0;
}
```
代码中使用了 `chrono` 库来计算排序算法的时间性能,使用了 `random_device` 和 `mt19937` 来生成随机数。可以根据自己的需求修改 `N` 的大小来测试不同规模数据的时间性能。
阅读全文