//这个排序把我整吐了,呕~!
堆排序讲得很好的例子
https://blog.csdn.net/qq_36573828/article/details/80261541?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1
#include<iostream>
#include<queue>
using namespace std;
void maopaosort(int a[], int n) { //冒泡排序
for (int i = 0; i < n - 1; i++) {
bool flag = false;
for (int j = 0; j < n - 1 - i; j++) {
if (a[j + 1] < a[j]) {
flag = true;
swap(a[j + 1], a[j]);
}
}
if (!flag)
break;
}
}
void insert_sort(int a[], int n) { //插入排序
int i, j;
for (int i = 1; i < n; i++) {
int tem = a[i];
for (j = i; j > 0 && a[j - 1] > tem; j--)
a[j] = a[j - 1];
a[j] = tem;
}
}
//希尔排序
void shellsort(int a[], int n) {
int i, j;
for (int d = n / 2; d > 0; d /= 2) {
for (i = d; i < n; i++) {
int tem = a[i];
for (j = i; j >= d && a[j - d] > tem; j -= d)
a[j] = a[j - d];
a[j] = tem;
}
}
}
// Sedgewick增量序列哈希排序
void Sedgewick_shell_sort(int a[], int n) {
int add[] = { 587521,260609,146305,64769,36289,16001,8929,3905,2161,929,505,209,109,41,19,5,1,0 };
int i = 0;
int j, k;
for (int d = add[i]; d > 0; d = add[++i]) {
for (j = d; j < n; j++) {
int tem = a[j];
for (k = j; k >= d && a[k - d] > tem; k -= d)
a[k] = a[k - d];
a[k] = tem;
}
}
}
//建立最大堆,每次取最大堆根节点与最后一个交换(important)
void precdown(int a[], int root, int num) {
int parent, child;
int tmp = a[root];
for (parent = root; parent * 2 + 1 < num; parent = child) {
child = parent * 2 + 1;
if ((child != num - 1) && a[child] < a[child + 1])
child++;
if (a[child] <= tmp)
break;
else
a[parent] = a[child];
}
a[parent] = tmp;
}//建立最大堆的时候从下往上从右到左,后面调整堆是从根节点开始调整
void heapsort(int a[], int n) {
for (int i = n / 2; i >= 0; i--)
precdown(a, i, n);
for (int i = n - 1; i > 0; i--) {
swap(a[0], a[i]);
precdown(a, 0, i);
}
}
//stl堆排序----------------------------------------------------------------------------------
priority_queue<int>q;
void stlheapsort(int a[], int n) {
for (int i = 0; i < n; i++)
q.push(a[i]);
for (int i = n - 1; i >= 0; i--) {
a[i] = q.top();
q.pop();
}
}
//非递归归并排序
void merge(int a[], int tem[], int l, int r, int rightend) {
int n = rightend - l + 1;
int leftend = r - 1;
int tmp = l;
while (l <= leftend && r <= rightend) {
if (a[l] <= a[r])
tem[tmp++] = a[l++];
else
tem[tmp++] = a[r++];
}
while (l <= leftend)
tmp[tem++] = a[l++];
while (r <= rightend)
tmp[tem++] = a[r++];
for (int i = 0; i < n; i++)
a[rightend--] = tmp[--tem];
}
//一趟归并
void merge_pass(int a[], int tem[], int n, int length) {
int i;
for (i = 0; i <= n - 2 * length; i += 2 * length)
merge(a, tem, i, i + length, i + 2 * length - 1);
if (i + length < n)//归并最后还剩下了两个子列(长度不相等)
merge(a, tem, i, i + length, n - 1);
else //最后只剩一个子列直接导入
for (int j = i; j < n; j++)
tem[j] = a[j];
}
void merge_sort(int a[], int n) {
int length = 1;
int* tem = new int[n];
if (tem != NULL) {
while (length < n) {
merge_pass(a, tem, n, length);
length *= 2;
}
delete[] tem;
}
else cout << "asdad";
}
//--------------------------------------------------------
int main() {
int n;
cin >> n;
int a[100000];
for (int i = 0; i < n; i++)
cin >> a[i];
merge_sort(a, n);
for (int i = 0; i < n; i++) {
if (i)cout << " ";
cout << a[i];
}
}
//```