快速排序源代码
#include <stdio.h>#include <stdlib.h>
FILE *fin,*fout;
void file_start()
{
if((fin=fopen("qsort.in","r"))==NULL)
{
printf("Cannot open this file!\n");
getchar();
exit(0);
}
if((fout=fopen("qsort.out","w"))==NULL)
{
printf("Cannot open this file!\n");
getchar();
exit(0);
}
}
void Qsort(int number[],int startPos, int endPos)
{
int i,j,temp;
temp=number[startPos];
i=startPos; j=endPos;
while(i<j)
{
while(number[j]>=temp && i<j)--j;
number[i]=number[j];
while(number[i]<=temp && i<j)++i;
number[j]=number[i];
}
number[i]=temp;
if(i-1>startPos) Qsort(number,startPos,i-1);
if(endPos>i+1) Qsort(number,i+1,endPos);
}
int main()
{
int number[100000]={0},n,i;
file_start();//方便输入输出,可以删除
fscanf(fin,"%d",&n);
for(i=0;i<n;i++)
fscanf(fin,"%d",&number[i]);
Qsort(number,0,n-1);
for(i=0;i<n;i++)
fprintf(fout,"%d ",number[i]);
system("pause");
return 0;
}