用c++语言随机产生1000-10000个数,分别用希尔排序、快速排序、归并排序和基数排序方法进行排序
时间: 2024-03-23 09:43:03 浏览: 62
好的,我可以为您提供C++语言实现。
```c++
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <chrono>
using namespace std;
// 希尔排序
void shell_sort(vector<int>& arr) {
int n = arr.size();
int gap = n / 2;
while (gap > 0) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
gap /= 2;
}
}
// 快速排序
void quick_sort(vector<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--;
}
while (i < j && arr[i] <= pivot) {
i++;
}
swap(arr[i], arr[j]);
}
swap(arr[left], arr[i]);
quick_sort(arr, left, i - 1);
quick_sort(arr, i + 1, right);
}
// 归并排序
vector<int> merge_sort(vector<int>& arr) {
int n = arr.size();
if (n <= 1) {
return arr;
}
int mid = n / 2;
vector<int> left(arr.begin(), arr.begin() + mid);
vector<int> right(arr.begin() + mid, arr.end());
left = merge_sort(left);
right = merge_sort(right);
vector<int> res;
int i = 0, j = 0;
while (i < left.size() && j < right.size()) {
if (left[i] <= right[j]) {
res.push_back(left[i++]);
} else {
res.push_back(right[j++]);
}
}
while (i < left.size()) {
res.push_back(left[i++]);
}
while (j < right.size()) {
res.push_back(right[j++]);
}
return res;
}
// 基数排序
void radix_sort(vector<int>& arr) {
int radix = 10;
int max_digit = 0;
for (int num : arr) {
max_digit = max(max_digit, (int)to_string(num).size());
}
for (int i = 0; i < max_digit; i++) {
vector<vector<int>> buckets(radix);
for (int num : arr) {
int digit = num / pow(radix, i) % radix;
buckets[digit].push_back(num);
}
arr.clear();
for (vector<int>& bucket : buckets) {
arr.insert(arr.end(), bucket.begin(), bucket.end());
}
}
}
int main() {
// 生成随机数
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(-10000, 10000);
int n = dis(gen) % 9001 + 1000;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
arr[i] = dis(gen);
}
// 希尔排序
auto start = chrono::steady_clock::now();
shell_sort(arr);
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "希尔排序用时:" << chrono::duration <double, milli> (diff).count() << " ms" << endl;
// 快速排序
start = chrono::steady_clock::now();
quick_sort(arr, 0, n - 1);
end = chrono::steady_clock::now();
diff = end - start;
cout << "快速排序用时:" << chrono::duration <double, milli> (diff).count() << " ms" << endl;
// 归并排序
start = chrono::steady_clock::now();
arr = merge_sort(arr);
end = chrono::steady_clock::now();
diff = end - start;
cout << "归并排序用时:" << chrono::duration <double, milli> (diff).count() << " ms" << endl;
// 基数排序
start = chrono::steady_clock::now();
radix_sort(arr);
end = chrono::steady_clock::now();
diff = end - start;
cout << "基数排序用时:" << chrono::duration <double, milli> (diff).count() << " ms" << endl;
return 0;
}
```
运行以上代码,可以得到每种排序算法的用时。注意,由于基数排序需要将数据转换为字符串类型,因此它的排序结果与其他排序算法的结果可能略有不同。
阅读全文