先把代码保存一下,回头有空再补上算法原理。
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 20;
int oldarray[N], newarray[N];
int tmp[N];//用于归并排序
int heap[N + 1], cnt;//用于堆排序
int q[N + 1];//用于希尔排序
vector<int> v[10];//用于基数排序
void BubbleSort() {
//冒泡排序
memcpy(newarray, oldarray, sizeof oldarray);
for (int i = 0;i < N - 1;i++) {
for (int j = 0;j < N - i - 1;j++) {
if (newarray[j] > newarray[j + 1]) {
swap(newarray[j], newarray[j + 1]);
}
}
}
for (int i = 0;i < N;i++) {
cout << newarray[i] << " ";
}
cout << endl;
}
void SelectSort() {
//选择排序
memcpy(newarray, oldarray, sizeof oldarray);
for (int i = 0;i < N;i++) {
int k = i;
for (int j = i + 1;j < N;j++) {
if (newarray[k] > newarray[j]) {
k = j;
}
}
if (k != i) {
swap(newarray[i], newarray[k]);
}
}
for (int i = 0;i < N;i++) {
cout << newarray[i] << " ";
}
cout << endl;
}
void InsertSort() {
//插入排序
vector<int> sortarray;
for (int i = 0;i < N;i++) {
auto idx = lower_bound(sortarray.begin(), sortarray.end(), oldarray[i]);
sortarray.insert(idx, oldarray[i]);
}
for (int i = 0;i < sortarray.size();i++) {
cout << sortarray[i] << " ";
}
cout << endl;
}
void MergeSort(int l,int r) {
//归并排序
if (l >= r) return;
int mid = l + r >> 1;
MergeSort(l, mid), MergeSort(mid + 1, r);
int k = 0, i = l, j = mid + 1;
while (i <= mid && j <= r) {
if (newarray[i] <= newarray[j]) tmp[k++] = newarray[i++];
else tmp[k++] = newarray[j++];
}
while (i <= mid) tmp[k++] = newarray[i++];
while (j <= r) tmp[k++] = newarray[j++];
for (int i = 0, j = l;j <= r;i++, j++) {
newarray[j] = tmp[i];
}
}
void QuickSort(int l,int r) {
//快速排序
if (l >= r) return;
int x = newarray[l];
int i = l - 1, j = r + 1;
while (i < j) {
do i++; while (newarray[i] < x);
do j--; while (newarray[j] > x);
if (i < j) swap(newarray[i], newarray[j]);
}
QuickSort(l, j);
QuickSort(j + 1, r);
}
void down(int u) {
int t = u;
if (2 * u <= cnt && heap[2 * u] < heap[t]) t = 2 * u;
if (2 * u + 1 <= cnt && heap[2 * u + 1] < heap[t]) t = 2 * u + 1;
if (t != u) {
swap(heap[t], heap[u]);
down(t);
}
}
void HeapSort() {
//堆排序
for (int i = 0;i < N;i++) heap[++cnt] = oldarray[i];
for (int i = N / 2;i >= 1;i--) down(i);
for (int i = 0;i < N;i++) {
newarray[i] = heap[1];
heap[1] = heap[cnt--];
down(1);
}
for (int i = 0;i < N;i++) {
cout << newarray[i] << " ";
}
cout << endl;
}
void ShellSort() {
//希尔排序
for (int i = 0;i < N;i++) q[i + 1] = oldarray[i];
int d, i, j;//d为间隔
for (d = N / 2;d >= 1;d /= 2) {
for (i = d + 1;i <= N;i++) {
if (q[i] < q[i - d]) {
q[0] = q[i];
for (j = i - d;j > 0 && q[0] < q[j];j -= d) {
q[j + d] = q[j];//后移操作
}
q[j + d] = q[0];
}
}
}
//输出排序后的结果
for (int i = 1;i <= N;i++) {
cout << q[i] << " ";
}
cout << endl;
}
int GetIntegerBit(int x) {
//获取整数的位数
int cnt = 0;
while (x) x /= 10, cnt++;
return cnt;
}
void RadixSort() {
//基数排序
memcpy(newarray, oldarray, sizeof oldarray);
int maxbit = 0;
for (int i = 0;i < N;i++) maxbit = max(maxbit, GetIntegerBit(newarray[i]));
int base = 1;
int s = 0;
for (int i = 0;i < maxbit;i++) {
s = 0;
for (int j = 0;j < 10;j++) v[j].clear();
for (int j = 0;j < N;j++) {
int c = newarray[j] / base % 10;
v[c].push_back(newarray[j]);
}
for (int j = 0;j < 10;j++) {
for (int k = 0;k < v[j].size();k++) {
newarray[s++] = v[j][k];
}
}
base *= 10;
}
for (int i = 0;i < N;i++) {
cout << newarray[i] << " ";
}
cout << endl;
}
int main() {
srand(time(NULL));
for (int i = 0;i < N;i++) oldarray[i] = rand()%100;
cout << "Before Sort:" << endl;
for (int i = 0;i < N;i++) {
cout << oldarray[i] << " ";
}
cout << endl;
cout << "After BubbleSort:" << endl;
BubbleSort();
cout << "After SelectionSort:" << endl;
SelectSort();
cout << "After InsertSort:" << endl;
InsertSort();
cout << "After MergeSort:" << endl;
memcpy(newarray, oldarray, sizeof oldarray);
MergeSort(0, N - 1);
for (int i = 0;i < N;i++) {
cout << newarray[i] << " ";
}
cout << endl;
cout << "After QuickSort:" << endl;
memcpy(newarray, oldarray, sizeof oldarray);
QuickSort(0, N - 1);
for (int i = 0;i < N;i++) {
cout << newarray[i] << " ";
}
cout << endl;
cout << "After HeapSort:" << endl;
HeapSort();
cout << "After ShellSort:" << endl;
ShellSort();
cout << "After RadixSort:" << endl;
RadixSort();
return 0;
}
运行结果:
# 赞赞赞!
欸你也搞排序算法了。。。
好像少了计数排序和桶排序。。。能加下么?
对哦,可以考虑下
嗯,加油!
# 💪
# (ง •_•)ง