#include
#include
/*算法思想*/
/*从一端要排序的地方,选择头元素为比较元素,然后从下一个位置
*开始比较,直到找到,比之大的,而在右端,找到比之小的 交换*/
int partion(int *a,int x,int y);
void swap(int *x,int *y);
void quicksort(int *a,int beg,int end){
int res;
if(beg>=end)
return;
res=partion(a,beg,end);
quicksort(a,beg,res-1);
quicksort(a,res+1,end);
}
int partion(int *a,int x,int y){
int q=x;
int m=a[x];
x++;
while(x <=y){
while(a[x]<=m)
x++;
while(a[y]>=m && y>=x )
y--;
if(x <= y)
swap(&a[x],&a[y]);
}
if(q!=y)
swap(&a[y],&a[q]);
return y;
}
void swap(int *x,int *y){
int b;
b=*x;
*x=*y;
*y=b;
}
int main(){
int x;
int a[10];
for(x=0;x<10;++x)
a[x]=11-x;
for(x=0;x<10;++x)
printf("%d,",a[x]);
printf("\n");
quicksort(a,0,9);
for(x=0;x<10;++x)
printf("%d,",a[x]);
}
阅读(277) | 评论(0) | 转发(0) |