给定N个(长整型范围内的)整数,要求输出从小到大排序后的结果。
本题旨在测试各种不同的排序算法在各种数据情况下的表现。各组测试数据特点如下:
- 数据1:只有1个元素;
- 数据2:11个不相同的整数,测试基本正确性;
- 数据3:103个随机整数;
- 数据4:104个随机整数;
- 数据5:105个随机整数;
- 数据6:105个顺序整数;
- 数据7:105个逆序整数;
- 数据8:105个基本有序的整数;
- 数据9:105个随机正整数,每个数字不超过1000。
输入格式:
输入第一行给出正整数N(≤105),随后一行给出N个(长整型范围内的)整数,其间以空格分隔。
输出格式:
在一行中输出从小到大排序后的结果,数字间以1个空格分隔,行末不得有多余空格。
输入样例:
11 4 981 10 -17 0 -20 29 50 8 43 -5
输出样例:
-20 -17 -5 0 4 8 10 29 43 50 981
采用四种排序算法进行排序
1.冒泡排序
测试结果
提交时间 | 状态 | 分数 | 题目 | 编译器 | 耗时 | 用户 |
---|---|---|---|---|---|---|
2020/06/18 14:25:26 | 部分正确
| 19 | 7-12 | C (gcc) | 5111 ms | Cnhhc |
测试点 | 结果 | 耗时 | 内存 |
---|---|---|---|
0 | 答案正确 | 2 ms | 256 KB |
1 | 答案正确 | 3 ms | 384 KB |
2 | 答案正确 | 4 ms | 256 KB |
3 | 答案正确 | 152 ms | 256 KB |
4 | 运行超时
| -- | 0 KB |
5 | 答案正确 | 5087 ms | 1320 KB |
6 | 运行超时
| -- | 0 KB |
7 | 答案正确 | 5111 ms | 1320 KB |
8 | 运行超时
| -- | 0 KB |
代码
#include <stdio.h>
#include <stdlib.h>
int main(){
int n=0;
scanf("%d",&n);
int* array=(int*)malloc(n*sizeof(int));
int i=0;
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
int j=0;
//int issort=0;
for(i=0;i<n-1;i++){
int temp;
for(j=n-1;j>i;j--){
if(array[j-1]>array[j]){
temp=array[j-1];
array[j-1]=array[j];
array[j]=temp;
// issort=1;
}
}
printf("%d ",array[i]);
// if(issort==0) break;
}
printf("%d",array[i]);
/* for(i=0;i<n-1;i++){
printf("%d ",array[i]);
}
printf("%d",array[i]);*/
}
2.希尔排序
测试结果
提交时间 | 状态 | 分数 | 题目 | 编译器 | 耗时 | 用户 |
---|---|---|---|---|---|---|
2020/06/18 14:29:02 | 答案正确 | 25 | 7-12 | C (gcc) | 55 ms | Cnhhc |
测试点 | 结果 | 耗时 | 内存 |
---|---|---|---|
0 | 答案正确 | 4 ms | 384 KB |
1 | 答案正确 | 4 ms | 256 KB |
2 | 答案正确 | 5 ms | 368 KB |
3 | 答案正确 | 9 ms | 424 KB |
4 | 答案正确 | 55 ms | 1320 KB |
5 | 答案正确 | 43 ms | 1152 KB |
6 | 答案正确 | 44 ms | 1280 KB |
7 | 答案正确 | 44 ms | 1152 KB |
8 | 答案正确 | 50 ms | 1048 KB |
采用的增量序列有Hibbard增量序列和Sedgewick增量序列,这里采用Sedgewick增量序列。
代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void shell_sort(int a[],int N);
int main(){
int N;
scanf("%d",&N);
int* a=(int*)malloc(N*sizeof(int));
int i;
for(i=0;i<N;i++)
scanf("%d",&a[i]);
shell_sort(a,N);
for(i=0;i<N-1;i++)
printf("%d ",a[i]);
printf("%d",a[i]);
free(a);
return 0;
}
void shell_sort(int a[],int N){
int i=0;
int k=0;
int seg[100];
while(1) {
if(9*pow(4,i)-9*pow(2,i)+1<N/2) {
seg[k]=9*pow(4,i)-9*pow(2,i)+1;
k++;
}
else break;
if(pow(4,i+2)-3*pow(2,i+2)+1<N/2){
seg[k]=pow(4,i+2)-3*pow(2,i+2)+1;
k++;
}
else break;
i++;
}
int D,p,temp;
for(;k>0;k--){
D=seg[k-1];
for(p=D;p<N;p++){
temp=a[p];
for(i=p;i>=D&&a[i-D]>temp;i-=D){
a[i]=a[i-D];
}
a[i]=temp;
}
}
}
3.堆排序
测试结果
提交时间 | 状态 | 分数 | 题目 | 编译器 | 耗时 | 用户 |
---|---|---|---|---|---|---|
2020/06/18 14:38:24 | 答案正确 | 25 | 7-12 | C (gcc) | 57 ms | Cnhhc |
测试点 | 结果 | 耗时 | 内存 |
---|---|---|---|
0 | 答案正确 | 4 ms | 224 KB |
1 | 答案正确 | 10 ms | 384 KB |
2 | 答案正确 | 5 ms | 256 KB |
3 | 答案正确 | 12 ms | 396 KB |
4 | 答案正确 | 56 ms | 1280 KB |
5 | 答案正确 | 55 ms | 1224 KB |
6 | 答案正确 | 50 ms | 1152 KB |
7 | 答案正确 | 48 ms | 1196 KB |
8 | 答案正确 | 57 ms | 1064 KB |
代码
#include <stdio.h>
#include <stdlib.h>
#define Inf 1E6
typedef struct heap* Heap;
struct heap{
int size;
int Maxsize;
int* data;
};
void heap_sort(Heap H);
int main(){
int N,i;
scanf("%d",&N);
Heap H=(Heap)malloc(sizeof(struct heap));
H->Maxsize=N+1;
H->size=N+1;
H->data=(int*)malloc(N*sizeof(int));
H->data[0]=Inf;
for(i=0;i<N;i++)
scanf("%d",&H->data[i+1]);
heap_sort(H);
for(i=0;i<N-1;i++)
printf("%d ",H->data[i+1]);
printf("%d",H->data[i+1]);
return 0;
}
void heap_sort(Heap H){
int i,j,temp,child;
for(i=2;i<H->Maxsize;i++){
temp=H->data[i];
for(j=i;j>1&&H->data[j/2]<temp;j/=2){
H->data[j]=H->data[j/2];
}
H->data[j]=temp;
}
for(i=1;i<H->Maxsize-1;i++){
temp=H->data[1];
H->data[1]=H->data[H->size-1];
H->data[H->size-1]=temp;
H->size--;
j=1;
while(1){
child=0;
if(2*j<H->size)
child=2*j;
if(2*j+1<H->size&&H->data[2*j+1]>H->data[2*j])
child=2*j+1;
if(!child||H->data[j]>=H->data[child])
break;
else{
temp=H->data[j];
H->data[j]=H->data[child];
H->data[child]=temp;
j=child;
}
}
}
}
4.归并排序
测试结果
提交时间 | 状态 | 分数 | 题目 | 编译器 | 耗时 | 用户 |
---|---|---|---|---|---|---|
2020/06/18 14:16:59 | 答案正确 | 25 | 7-12 | C (gcc) | 64 ms | Cnhhc |
测试点 | 结果 | 耗时 | 内存 |
---|---|---|---|
0 | 答案正确 | 2 ms | 360 KB |
1 | 答案正确 | 2 ms | 268 KB |
2 | 答案正确 | 2 ms | 256 KB |
3 | 答案正确 | 7 ms | 424 KB |
4 | 答案正确 | 53 ms | 1272 KB |
5 | 答案正确 | 44 ms | 1144 KB |
6 | 答案正确 | 45 ms | 1244 KB |
7 | 答案正确 | 44 ms | 1264 KB |
8 | 答案正确 | 64 ms | 1024 KB |
代码
#include <stdio.h>
#include <stdlib.h>
void M_Sort(int a[],int N);
void Merge(int a[],int temp[],int L,int R,int RightEnd);
void MSort(int a[],int temp[],int sp,int tp);
int main(){
int N,i;
scanf("%d",&N);
int *a=(int*)malloc(N*sizeof(int));
for(i=0;i<N;i++)
scanf("%d",&a[i]);
M_Sort(a,N);
for(i=0;i<N-1;i++)
printf("%d ",a[i]);
printf("%d",a[i]);
free(a);
return 0;
}
void M_Sort(int a[],int N){
int* temp=(int*)malloc(sizeof(int)*N);
MSort(a,temp,0,N-1);
free(temp);
}
void Merge(int a[],int temp[],int L,int R,int RightEnd){
int LeftEnd=R-1;
int i=L;
int Num=RightEnd-L+1;
while(L<=LeftEnd&&R<=RightEnd){
if(a[L]<=a[R]) temp[i++]=a[L++];
else temp[i++]=a[R++];
}
while(L<=LeftEnd)
temp[i++]=a[L++];
while(R<=RightEnd)
temp[i++]=a[R++];
int j;
for(j=0;j<Num;j++){
i--;
a[i]=temp[i];
}
}
void MSort(int a[],int temp[],int sp,int tp){
int center=(sp+tp)/2;
if(tp>sp){
MSort(a,temp,sp,center);
MSort(a,temp,center+1,tp);
Merge(a,temp,sp,center+1,tp);
}
}