快速排序,递归实现:
typedef int elemtype;
int partition(elemtype*,int,int);
void QuickSort(elemtype*,int,int);
void main(){
int count,i=0;
elemtype array[MAXNUM+1];
puts("the count of the number:");
scanf("%d",&count);
puts("the numbers:");
while(count--) scanf("%d",&array[++i]); //0号不使用
QuickSort(array,1,i);
for(count=1;count<=i;count++) printf("%d ",array[count]);
}
void QuickSort(elemtype array[],int low,int high){ //快排主函数,递归
int pivot; //中枢
if(low<high){
pivot=partition(array,low,high);
QuickSort(array,low,pivot-1);
QuickSort(array,pivot+1,high);
}//if
}//QuickSort
int partition(elemtype array[],int low,int high){ //进行一次快排
array[0]=array[low]; //暂存,且将之用作中枢
while(low<high){
while(low<high&&array[high]>=array[0]) high--;
array[low]=array[high];
while(low<high&&array[low]<array[0]) low++;
array[high]=array[low];
}//while
array[low]=array[0];
return low;
}