归并排序:
#include"pch.h"
#include<iostream>
#include<cmath>
using namespace std;
void Merge(int a[], int s, int m, int e, int tmp[]) {
int pb = 0;
int p1 = s, p2 = m + 1;
while (p1 <= m && p2 <= e) {
if (a[p1] < a[p2]) {
tmp[pb++] = a[p1++];
}
else {
tmp[pb++] = a[p2++];
}
}
while (p1 <= m)
tmp[pb++] = a[p1++];
while (p2 <= e)
tmp[pb++] = a[p2++];
for (int i = 0; i < e - s + 1; ++i) {
a[s + i] = tmp[i];
}
}
void MergeSort(int a[], int s, int e, int tmp[]) {
if (s < e) {
int m = s + (e - s) / 2;
MergeSort(a, s, m, tmp);
MergeSort(a, m + 1, e, tmp);
Merge(a, s, m, e, tmp);
}
}
int a[10] = { 13,27,19,2,8,12,2,8,30,89 };
int b[10];
int main() {
int size = sizeof(a) / sizeof(int);
MergeSort(a, 0, size - 1,b);
for (int i = 0; i < size; ++i)
cout << a[i] << ",";
cout << endl;
return 0;
}
快速排序:
#include"pch.h"
#include<iostream>
#include<cmath>
using namespace std;
void swap(int &a, int &b) {
int tmp = a;
a = b;
b = tmp;
}
void QuickSort(int a[], int s, int e) {
if (s >= e) return;
int k = a[s];
int i = s, j = e;
while (i != j) {
while (j > i && a[j] >= k)
--j;
swap(a[i], a[j]);
while (i < j &&a[i] <= k)
++i;
swap(a[i], a[j]);
}
QuickSort(a, s, i - 1);
QuickSort(a, i + 1, e);
}
int a[] = { 93,27,30,2,8,12,2,8,30,89 };
int main() {
int size = sizeof(a) / sizeof(int);
QuickSort(a, 0, size - 1);
for (int i = 0; i < size; ++i) {
cout << a[i] << ",";
}
cout << endl;
return 0;
}