QuickSort(快速排序)Source(C++/C)
Posted by Javran on 01月 2nd, 2008
感谢Felix提供的C++源代码!
#include <stdio.h>
#include <stdlib.h>
#define N 1010
void sort(int *a,int start,int end);
int main()
{
int s[N],n;
while(scanf(”%d”,&n)!=EOF)
{
for(int i=0;i<n;i++)scanf(”%d”,&s[i]);
sort(s,0,n-1);
for(int i=0;i<n;i++)printf(”%d “,s[i]);
printf(”\n”);
}
return 0;
}
void sort(int *a,int start,int end)
{
int i=start,j=end,temp;
if(start<end)
{
temp=a[start];
while(i!=j)
{
while(i<j && a[j]>temp)j–;
if(i<j)a[i]=a[j],i++;
while(i<j && a[i]<temp)i++;
if(i<j)a[j]=a[i],j–;
}
a[i]=temp;
sort(a,start,i-1);
sort(a,i+1,end);
}
}


01月 8th, 2008 at 20:05
真不错,用linux系统?
01月 8th, 2008 at 21:42
我用Linux?怎么回事?
08月 4th, 2008 at 23:32
1L的评论真是神奇。。。
话说现在我都不写qsort函数了,都用STL的sort(), 随机化的qsort,保证了不会有最坏情况的n*n出现。
08月 5th, 2008 at 22:38
我也都用随机的了,毕竟基本不可能退化啊