能不能换成c++语言实现希尔排序、快速排序、归并排序和基数排序的代码。
时间: 2024-03-11 21:51:42 浏览: 82
当然可以,以下是使用 C++ 语言实现希尔排序、快速排序、归并排序和基数排序的代码。
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 希尔排序
vector<int> 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;
}
return arr;
}
// 快速排序
vector<int> quick_sort(vector<int>& arr) {
if (arr.size() <= 1) {
return arr;
}
int pivot = arr[arr.size() / 2];
vector<int> left, middle, right;
for (int i = 0; i < arr.size(); i++) {
if (arr[i] < pivot) {
left.push_back(arr[i]);
} else if (arr[i] == pivot) {
middle.push_back(arr[i]);
} else {
right.push_back(arr[i]);
}
}
left = quick_sort(left);
right = quick_sort(right);
vector<int> res;
res.reserve(left.size() + middle.size() + right.size());
res.insert(res.end(), left.begin(), left.end());
res.insert(res.end(), middle.begin(), middle.end());
res.insert(res.end(), right.begin(), right.end());
return res;
}
// 归并排序
vector<int> merge_sort(vector<int>& arr) {
if (arr.size() <= 1) {
return arr;
}
int mid = arr.size() / 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(left.size() + right.size());
int i = 0, j = 0, k = 0;
while (i < left.size() && j < right.size()) {
if (left[i] < right[j]) {
res[k++] = left[i++];
} else {
res[k++] = right[j++];
}
}
while (i < left.size()) {
res[k++] = left[i++];
}
while (j < right.size()) {
res[k++] = right[j++];
}
return res;
}
// 基数排序
vector<int> radix_sort(vector<int>& arr) {
int max_num = *max_element(arr.begin(), arr.end());
int exp = 1;
vector<int> output(arr.size());
while (max_num / exp > 0) {
vector<int> count(10);
for (int i = 0; i < arr.size(); i++) {
int index = arr[i] / exp % 10;
count[index]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = arr.size() - 1; i >= 0; i--) {
int index = arr[i] / exp % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
for (int i = 0; i < arr.size(); i++) {
arr[i] = output[i];
}
exp *= 10;
}
return arr;
}
// 测试代码
int main() {
vector<int> arr(1000);
for (int i = 0; i < 1000; i++) {
arr[i] = rand() % 9001 + 1000;
}
cout << "原始数组: ";
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
vector<int> arr_shell = shell_sort(arr);
cout << "希尔排序结果: ";
for (int i = 0; i < arr_shell.size(); i++) {
cout << arr_shell[i] << " ";
}
cout << endl;
vector<int> arr_quick = quick_sort(arr);
cout << "快速排序结果: ";
for (int i = 0; i < arr_quick.size(); i++) {
cout << arr_quick[i] << " ";
}
cout << endl;
vector<int> arr_merge = merge_sort(arr);
cout << "归并排序结果: ";
for (int i = 0; i < arr_merge.size(); i++) {
cout << arr_merge[i] << " ";
}
cout << endl;
vector<int> arr_radix = radix_sort(arr);
cout << "基数排序结果: ";
for (int i = 0; i < arr_radix.size(); i++) {
cout << arr_radix[i] << " ";
}
cout << endl;
return 0;
}
```
以上代码中,我们使用 C++ 自带的 vector 容器和 algorithm 库中的 max_element 函数来实现希尔排序、快速排序、归并排序和基数排序。在测试时,你可以将数组长度改为 10000,或者更大的数值,以测试算法的效率。
阅读全文