//排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 100005
void Swap(int *a, int *b) {
int t = *a; *a = *b; *b = t;
}
void Bubble_Sort(int a[], int n) {
int i, j, flag;
for(i = n; i > 0; i--) {
flag = 1;
for(j = 1; j < i; j++) {
if(a[j-1] > a[j]) {
Swap(&a[j], &a[j+1]);
flag = 0;
}
}
if(flag) break;
}
}
void Insertion_Sort(int a[], int n) {
int i, j, tmp;
for(i = 1; i < n; i++) {
tmp = a[i];
for(j = i; a[j-1] > tmp && j > 0; j--) a[j] = a[j-1];//其实j>=1
a[j] = tmp;
}
}
void ShellSort(int a[], int n) {
//int Sedgewick[] = {929, 505, 209, 109, 41, 19, 5, 1, 0};//0作为结束的标志
int dist[MAXN] = {(1 << 10)-1};//Hibbard增量序列,卧槽忘了指定数组大小。
int d, i, Si, tmp, j, flag;
//for(Si = 0; Sedgewick[Si] >= n; Si++);
//for(d = Sedgewick[Si]; d > 0; d = Sedgewick[++Si])
for(i = 0; dist[i]; i++) dist[i+1] = (dist[i]+1)/2-1;
//for(i = 0; dist[i]; i++) printf("%d ", dist[i]);
for(Si = 0; dist[Si] >= n; Si++);
for(d = dist[Si]; d > 0; d = dist[++Si])
for(i = d; i < n; i++) {//插入排序
tmp = a[i];
for(j = i; j >= d && a[j-d] > tmp; j -= d) a[j] = a[j-d];
a[j] = tmp;
}/*
for(i = n; i > 0; i -= d) {//冒泡排序
flag = 1;
for(j = d; j < i; j += d) {
if(a[j-d] > a[j]) {
Swap(&a[j-d], &a[j]);
flag = 0;
}
}
if(flag) break;
}*/
}
void PercDown(int a[], int p, int n) {
int tmp = a[p];
int parent, child;
for(parent = p; 2*parent+1 < n; parent = child) {
child = 2*parent+1;
if(child != n-1 && a[child] < a[child+1]) child++;
if(tmp > a[child]) break;
a[parent] = a[child];
}
a[parent] = tmp;
}
void HeapSort(int a[], int n) {
int i;
//建立最大堆
for(i = n/2-1; i >= 0; i--) PercDown(a, i, n);
for(i = n-1; i>0; i--) {
Swap(&a[0], &a[i]);
PercDown(a, 0, i);
}
}
void Merge(int a[], int temp[], int ll, int rl, int rh) {
int lh = rl-1, tl = ll, len = rh - ll + 1, i;
while(ll <= lh && rl <= rh) {
if(a[ll] <= a[rl]) temp[tl++] = a[ll++];
else temp[tl++] = a[rl++];
}
while(ll <= lh) temp[tl++] = a[ll++];
while(rl <= rh) temp[tl++] = a[rl++];
//for(i = 0; i < len; i++, rh--) a[rh] = temp[rh];
}
void M_Sort(int a[], int temp[], int l, int r) {
int mid = (l+r)/2;
if(l != r) {
M_Sort(a, temp, l, mid);
M_Sort(a, temp, mid+1, r);
Merge(a, temp, l, mid+1, r);
}
}
void Merge_Sort(int a[], int n) {
int temp[MAXN];
M_Sort(a, temp, 0, n-1);
}
void Merge_pass(int a[], int temp[], int n, int length) {
int i, j;
for(i = 0; i <= n-2*length; i += 2*length)
Merge(a, temp, i, i+length, i+2*length-1);
if(i+length < n)//貌似归并不需要两端等长
Merge(a, temp, i, i+length, n-1);
else
for(j = i; j < n; j++) temp[j] = a[j];
}
void Merge_Sort_Again(int a[], int n) {
int temp[MAXN], length = 1;
while(length < n) {//保证结果存在a[]
Merge_pass(a, temp, n, length);
length *= 2;
Merge_pass(temp, a, n, length);
length *= 2;
}
}
int a[MAXN];
int main() {
int n, i, flag = 1;
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d", &n);
for(i = 0; i < n; i++) scanf("%d", &a[i]);
//Bubble_Sort(a, n);
//Insertion_Sort(a, n);
//ShellSort(a, n);
//HeapSort(a, n);
//Merge_Sort(a, n);
Merge_Sort_Again(a, n);
for(i = 0; i < n; i++) {
if(flag) flag = 0;
else printf(" ");
printf("%d", a[i]);
}
return 0;
}
/*
Bubble_Sort:稳定,但是,很慢
Insertion_Sort:稳定,排有序的数蛮快的额。。
ShellSort:速度不错,但是不稳定,为什么用冒泡排序写的shell排序会超时呢。。
Sedgewick增量序列
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 1/1 2 1
测试点2 答案正确 10/10 1 1
测试点3 答案正确 2/2 3 1
测试点4 答案正确 2/2 19 1
测试点5 答案正确 2/2 56 2
测试点6 答案正确 2/2 39 2
测试点7 答案正确 2/2 53 2
测试点8 答案正确 2/2 40 2
测试点9 答案正确 2/2 50 1
Hibbard增量序列:
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 1/1 6 1
测试点2 答案正确 10/10 3 1
测试点3 答案正确 2/2 3 1
测试点4 答案正确 2/2 13 1
测试点5 答案正确 2/2 84 2
测试点6 答案正确 2/2 45 2
测试点7 答案正确 2/2 71 2
测试点8 答案正确 2/2 54 2
测试点9 答案正确 2/2 73 2
HeapSort:好处在于可以快速找到一个序列中前m个最大或最小元素
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 1/1 2 1
测试点2 答案正确 10/10 2 1
测试点3 答案正确 2/2 2 1
测试点4 答案正确 2/2 29 1
测试点5 答案正确 2/2 55 2
测试点6 答案正确 2/2 49 2
测试点7 答案正确 2/2 50 2
测试点8 答案正确 2/2 49 2
测试点9 答案正确 2/2 51 1
Merge_Sort:稳定,外排序非常有用。
递归实现:
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 1/1 2 1
测试点2 答案正确 10/10 2 1
测试点3 答案正确 2/2 2 1
测试点4 答案正确 2/2 7 1
测试点5 答案正确 2/2 65 2
测试点6 答案正确 2/2 44 2
测试点7 答案正确 2/2 44 2
测试点8 答案正确 2/2 44 2
测试点9 答案正确 2/2 61 2
非递归实现:效果比递归好些
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 1/1 2 1
测试点2 答案正确 10/10 3 1
测试点3 答案正确 2/2 2 1
测试点4 答案正确 2/2 6 1
测试点5 答案正确 2/2 49 2
测试点6 答案正确 2/2 43 2
测试点7 答案正确 2/2 47 2
测试点8 答案正确 2/2 43 2
测试点9 答案正确 2/2 49 2
*/
09-排序1 排序 (25分)
最新推荐文章于 2021-03-21 00:32:06 发布