在VC++的环境下用C/C++实现直接插入排序、希尔排序、快速排序、冒泡排序、堆排序、选择排序、基数排序,输入待排序记录,分别用内部排序的几种排序方法对其排序并输出
时间: 2023-08-10 20:06:09 浏览: 110
好的,我可以为您解答这个问题。
以下是您所提到的七种排序算法的C/C++代码实现,每个算法均有对应的注释说明。其中,基数排序的实现使用了队列(Queue)和链表(List)两种数据结构。
```C++
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// 直接插入排序
void InsertionSort(vector<int>& nums) {
int n = nums.size();
for (int i = 1; i < n; ++i) {
int key = nums[i];
int j = i - 1;
while (j >= 0 && nums[j] > key) {
nums[j + 1] = nums[j];
--j;
}
nums[j + 1] = key;
}
}
// 希尔排序
void ShellSort(vector<int>& nums) {
int n = nums.size();
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; ++i) {
int key = nums[i];
int j = i;
while (j >= gap && nums[j - gap] > key) {
nums[j] = nums[j - gap];
j -= gap;
}
nums[j] = key;
}
}
}
// 快速排序
void QuickSort(vector<int>& nums, int left, int right) {
if (left >= right) return;
int i = left, j = right;
int pivot = nums[left];
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;
QuickSort(nums, left, i - 1);
QuickSort(nums, i + 1, right);
}
// 冒泡排序
void BubbleSort(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n - 1; ++i) {
bool flag = false;
for (int j = 0; j < n - i - 1; ++j) {
if (nums[j] > nums[j + 1]) {
swap(nums[j], nums[j + 1]);
flag = true;
}
}
if (!flag) break;
}
}
// 堆排序
void HeapSort(vector<int>& nums) {
int n = nums.size();
for (int i = n / 2 - 1; i >= 0; --i) {
int j = i;
while (2 * j + 1 < n) {
int k = 2 * j + 1;
if (k + 1 < n && nums[k + 1] > nums[k]) ++k;
if (nums[k] > nums[j]) {
swap(nums[k], nums[j]);
j = k;
} else {
break;
}
}
}
for (int i = n - 1; i > 0; --i) {
swap(nums[0], nums[i]);
int j = 0;
while (2 * j + 1 < i) {
int k = 2 * j + 1;
if (k + 1 < i && nums[k + 1] > nums[k]) ++k;
if (nums[k] > nums[j]) {
swap(nums[k], nums[j]);
j = k;
} else {
break;
}
}
}
}
// 选择排序
void SelectionSort(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n - 1; ++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 RadixSort(vector<int>& nums) {
int n = nums.size();
int max_val = *max_element(nums.begin(), nums.end());
int d = 1; // 位数
while (max_val /= 10) ++d;
queue<int> q[10];
for (int i = 0; i < d; ++i) {
for (int j = 0; j < n; ++j) {
int k = nums[j] / pow(10, i) % 10;
q[k].push(nums[j]);
}
int idx = 0;
for (int j = 0; j < 10; ++j) {
while (!q[j].empty()) {
nums[idx++] = q[j].front();
q[j].pop();
}
}
}
}
int main() {
vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
cout << "Original: ";
for (int x : nums) cout << x << " ";
cout << endl;
InsertionSort(nums);
cout << "InsertionSort: ";
for (int x : nums) cout << x << " ";
cout << endl;
nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
ShellSort(nums);
cout << "ShellSort: ";
for (int x : nums) cout << x << " ";
cout << endl;
nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
QuickSort(nums, 0, nums.size() - 1);
cout << "QuickSort: ";
for (int x : nums) cout << x << " ";
cout << endl;
nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
BubbleSort(nums);
cout << "BubbleSort: ";
for (int x : nums) cout << x << " ";
cout << endl;
nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
HeapSort(nums);
cout << "HeapSort: ";
for (int x : nums) cout << x << " ";
cout << endl;
nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
SelectionSort(nums);
cout << "SelectionSort: ";
for (int x : nums) cout << x << " ";
cout << endl;
nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
RadixSort(nums);
cout << "RadixSort: ";
for (int x : nums) cout << x << " ";
cout << endl;
return 0;
}
```
您可以将这段代码复制到Visual Studio等开发环境中进行编译运行,输入待排序记录后即可看到各个排序算法的输出结果。
阅读全文