输入格式:
输入第一行给出正整数N(N≤100000),随后一行给出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
方法一 选择排序
核心思想:在未排序的序列中选出最小的元素和序列的首位元素交换,接下来在剩下未排序的序列选出最小元素与序列的第二位元素交换,以此类推。
时间复杂度:O(N^2) 稳定
#include<stdio.h>
void SampleSelectSort(int* a,int n)
{
int i,j,min,temp;
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
if(a[j]<a[min])
min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("%d",a[0]);
for(i=1;i<n;i++)
printf(" %d",a[i]);
}
int main()
{
int i,n,a[100000];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
SampleSelectSort(a,n);
return 0;
}
用时:3536KB
内存占用:1200KB
方法二 冒泡排序
核心思想:将最大值通过交换移向一端,在下一次循环中不再考虑。
时间复杂度:O(N^2) 稳定
#include<stdio.h>
void BubbleSort(int*a,int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
printf("%d",a[0]);
for(i=1;i<n;i++)
printf(" %d",a[i]);
}
int main()
{
int i,n,a[100000];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
BubbleSort(a,n);
return 0;
}
运行超时
方法三 插入排序
核心思想:将待排序列分为未排序和已排序部分,初始状态,已排序的序列仅包含第一个元素,未排序序列有n-1个元素(除去第一个元素),此后将未排序的序列中的元素逐一插入已排序序列中。
时间复杂度:O(N^2) 稳定
#include<stdio.h>
void InsertionSort(int* a,int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i;j>0&&temp<a[j-1];j--)
a[j]=a[j-1];
a[j]=temp;
}
printf("%d",a[0]);
for(i=1;i<n;i++)
printf(" %d",a[i]);
}
int main()
{
int i,n,a[100000];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
InsertionSort(a,n);
return 0;
}
运行用时:2556ms
内存占用:1216KB
方法四 堆排序
核心思想:利用最大堆(最小堆)输出堆顶元素,将剩余元素重新生成最大堆,继续输出堆顶元素,重复此过程。
时间复杂度:O(NlogN) 不稳定
堆排序在处理数据量很大的时候程序运行速度非常快。
#include<stdio.h>
void Adjust(int*a,int i,int n)
{
int Child;
int temp;
for(temp=a[i];(2*i+1)<n;i=Child)
{
Child=(2*i+1);
if((Child!=n-1)&&a[Child+1]>a[Child])
Child++;
if(temp<a[Child])
a[i]=a[Child];
else break;
}
a[i]=temp;
}
void HeapSort(int* a,int n)
{
int i,temp;
for(i=(n-1)/2;i>=0;i--)
Adjust(a,i,n);
for(i=n-1;i>0;i--)
{
temp=a[0];
a[0]=a[i];
a[i]=temp;
Adjust(a,0,i);
}
printf("%d",a[0]);
for(i=1;i<n;i++)
printf(" %d",a[i]);
}
int main()
{
int i,n,a[100000];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
HeapSort(a,n);
return 0;
}
运行时间:69ms
占用内存:1400KB
方法五 快速排序
核心思想:将未排序的元素根据基准(自己选取)分成两个子序列,其中一个子序列的记录均大于基准,而另一个子序列均小于基准,然后递归对两个子序列用类似方法排序。
时间复杂度:O(NlogN)(情况好时) 不稳定
#include<stdio.h>
void Swap(int* a,int* b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void QSort(int* a,int low,int hight)
{
int pivot=a[low];
int left=low,right=hight;
if(low>=hight) return;
Swap(&a[low],&a[right]);
while(1)
{
while((low<hight)&&(pivot>=a[low]))
low++;
while((low<hight)&&(pivot<=a[hight]))
hight--;
if(low<hight)
Swap(&a[low],&a[hight]);
else break;
}
Swap(&a[low],&a[right]);
QSort(a,left,low-1);
QSort(a,low+1,right);
}
void QuickSort(int* a,int n)
{
int i=1;
QSort(a,0,n-1);
printf("%d",a[0]);
for(i=1;i<n;i++)
printf(" %d",a[i]);
}
int main()
{
int i,n,a[100000];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
QuickSort(a,n);
return 0;
}
运行时间:6679ms
占用内存:3592KB
方法六 归并排序
核心思想:将大小为N的序列看成N个长度为1的子序列,接下来将相邻子序列两两进行归并操作,形成N/2个长度为2的有序序列,然后再继续进行相邻子序列两两归并操作,如此一直循环,直到剩下1个长度为N的序列,则序列为原序列完成排序后的结果。
#include<stdio.h>
#include<stdlib.h>
void Merge( int* a,int* tempa,int L,int R, int RightEnd )
{ /* 将有序的A[L]~A[R-1]和A[R]~A[RightEnd]归并成一个有序序列 */
int LeftEnd,NumElements,temp;
int i;
LeftEnd = R - 1;
temp = L;
NumElements = RightEnd - L + 1;
while( L <= LeftEnd && R <= RightEnd ) {
if ( a[L] <= a[R] )
tempa[temp++]=a[L++];
else
tempa[temp++]=a[R++];
}
while( L <= LeftEnd )
tempa[temp++]=a[L++];
while( R <= RightEnd )
tempa[temp++]=a[R++];
for( i = 0; i < NumElements; i++, RightEnd -- )
a[RightEnd]=tempa[RightEnd]; /* 将有序的TmpA[]复制回A[] */
}
void Msort(int* a,int* tempa,int L,int RightEnd)
{ /* 核心递归排序函数 */
int Center;
if ( L < RightEnd ) {
Center = (L+RightEnd) / 2;
Msort(a,tempa,L,Center); /* 递归解决左边 */
Msort(a,tempa,Center+1,RightEnd); /* 递归解决右边 */
Merge(a,tempa,L,Center+1,RightEnd); /* 合并两段有序序列 */
}
}
void MergeSort(int* a,int n)
{ /* 归并排序 */
int* tempa;
tempa=(int*)malloc(n*sizeof(int));
if ( tempa != NULL ) {
Msort( a, tempa, 0, n-1 );
free( tempa );
}
else printf( "空间不足" );
}
int main()
{
int i,n,a[100000];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
MergeSort(a,n);
printf("%d",a[0]);
for(i=1;i<n;i++)
printf(" %d",a[i]);
return 0;
}
运行时间:66ms
占用内存:1360KB
方法七 希尔排序
核心思想:在插入排序的基础上的推广。
#include<stdio.h>
void ShellSort(int* a,int n)
{
int Incre[3]={5,3,1};
int i,j,m,k,increment,temp;
m=3;
for(i=0;i<m;i++)
{
increment=Incre[i];
for(j=increment;j<n;j++)
{
temp=a[j];
for(k=j;k-increment>=0&&temp<a[k-increment];k-=increment)
a[k]=a[k-increment];
a[k]=temp;
}
}
}
int main()
{
int i,n,a[100000];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ShellSort(a,n);
printf("%d",a[0]);
for(i=1;i<n;i++)
printf(" %d",a[i]);
return 0;
}
运行时间:1716ms
占用内存:1232KB