给定N个(长整型范围内的)整数,要求输出从小到大排序后的结果。
本题旨在测试各种不同的排序算法在各种数据情况下的表现。各组测试数据特点如下:
- 数据1:只有1个元素;
- 数据2:11个不相同的整数,测试基本正确性;
- 数据3:103个随机整数;
- 数据4:104个随机整数;
- 数据5:105个随机整数;
- 数据6:105个顺序整数;
- 数据7:105个逆序整数;
- 数据8:105个基本有序的整数;
- 数据9:105个随机正整数,每个数字不超过1000。
输入格式:
输入第一行给出正整数N(≤10
5
),随后一行给出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
代码示例(冒泡、插入、希尔、堆、归并):
#include<stdio.h>
#include<stdlib.h>
typedef long ElementType;
void Bubble_Sort(ElementType A[], int N);
void Insertion_Sort(ElementType A[], int N);
void Shell_Sort(ElementType A[], int N);
void Heap_Sort(ElementType A[],int N);
void Merge_pass( ElementType A[], ElementType TmpA[], int N, int length );
void Merge_Sort( ElementType A[], int N );
int main()
{
int N,i;
ElementType *arr;
scanf("%d",&N);
arr = (ElementType*)malloc(sizeof(ElementType) * N);
for(i = 0; i < N; i++){
scanf("%ld",&arr[i]);
}
// Bubble_Sort(arr,N);
// Insertion_Sort(arr,N);
// Shell_Sort(arr,N);
// Heap_Sort(arr,N);
Merge_Sort(arr,N);
for(i = 0; i < N; i++){
if(!i)
printf("%ld",arr[i]);
else
printf(" %ld",arr[i]);
}
}
//冒泡排序,O(n^2),优点:可以在链表中实现而且稳定,缺点就是慢
void Bubble_Sort(ElementType A[], int N)
{
int i,j;
ElementType temp;
for(i = 0; i < N - 1; i++){
for(j = 0; j < N-i-1; j++){
if(A[j] > A[j+1]){
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
}
//插入排序,O(n^2),稳定
void Insertion_Sort(ElementType A[], int N)
{
int P,i;
ElementType temp;
for(P = 1; P < N; P++){
temp = A[P];//保存要比较的元素
for(i = P; i > 0 && A[i-1] > temp; i--){//当temp是比前一个元素小的时候,前面的元素往后移位
A[i] = A[i-1];
}
A[i] = temp;
}
}
//希尔排序,
void Shell_Sort(ElementType A[], int N)
{
int P,D,i,Si;
ElementType temp;
int Sedgewick[] = {146305,64769,36289,16001,8929,3905,2161,929, 505, 209, 109, 41, 19, 5, 1, 0};
//插入排序的改版
for ( Si=0; Sedgewick[Si] >= N; Si++ ); /* 初始的增量Sedgewick[Si]不能超过待排序列长度 */
for(; Sedgewick[Si] > 0; Si++){
D = Sedgewick[Si];//这里不能写到上面初始化哦
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;
}
}
}
void PercDown( ElementType A[], int p, int N )
{
int Parent,Child;
ElementType temp = A[p];
for(Parent = p; (Parent * 2 + 1) < N; Parent = Child){
Child = Parent * 2 + 1;
if(Child + 1 != N && A[Child + 1] > A[Child]){
Child++;
}
if(temp >= A[Child])
break;
else
A[Parent] = A[Child];
}
A[Parent] = temp;
}
void Swap(ElementType *a,ElementType *b){
ElementType c;
c = *a;
*a = *b;
*b = c;
}
/* L = 左边起始位置, R = 右边起始位置, RightEnd = 右边终点位置*/
void Merge( ElementType A[], ElementType TmpA[], int L, int R, int RightEnd )
{ /* 将有序的A[L]~A[R-1]和A[R]~A[RightEnd]归并成一个有序序列 */
int LeftEnd, NumElements, Tmp;
int i;
LeftEnd = R - 1; /* 左边终点位置 */
Tmp = L; /* 有序序列的起始位置 */
NumElements = RightEnd - L + 1;
while( L <= LeftEnd && R <= RightEnd ) {
if ( A[L] <= A[R] )
TmpA[Tmp++] = A[L++]; /* 将左边元素复制到TmpA */
else
TmpA[Tmp++] = A[R++]; /* 将右边元素复制到TmpA */
}
while( L <= LeftEnd )
TmpA[Tmp++] = A[L++]; /* 直接复制左边剩下的 */
while( R <= RightEnd )
TmpA[Tmp++] = A[R++]; /* 直接复制右边剩下的 */
for( i = 0; i < NumElements; i++, RightEnd -- )
A[RightEnd] = TmpA[RightEnd]; /* 将有序的TmpA[]复制回A[] */
}
//堆排序,挺快
void Heap_Sort(ElementType 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_Sort( ElementType A[], int N )
{
int length;
ElementType *TmpA;
length = 1; /* 初始化子序列长度*/
TmpA = (ElementType*)malloc( N * sizeof( ElementType ) );
if ( TmpA != NULL ) {
while( length < N ) {
Merge_pass( A, TmpA, N, length );
length *= 2;
Merge_pass( TmpA, A, N, length );
length *= 2;
}
free( TmpA );
}
else printf( "空间不足" );
}
void Merge_pass( ElementType A[], ElementType TmpA[], int N, int length )
{ /* 两两归并相邻有序子列 */
int i, j;
for ( i=0; i <= N-2*length; i += 2*length )
Merge( A, TmpA, i, i+length, i+2*length-1 );
if ( i+length < N ) /* 归并最后2个子列*/
Merge( A, TmpA, i, i+length, N-1);
else /* 最后只剩1个子列*/
for ( j = i; j < N; j++ ) TmpA[j] = A[j];
}