运用了递归的思想
递归就是只管第一次,因为第一次结果都是对的,所以接下来的结果都是正确的,只需要等待递归返回最终结果就行。
int a[10] = {0,6,4,7,9,3,2,5,8,1};
- (void)fast{
[self quciksort:1 and:9];
for (int i =1; i<=9; i++) {
printf("%d",a[i]);
}
}
- (void)quciksort:(int)left and:(int)right{
int i,j,t,temp;
if (left>right)
return;
temp =a[left];//temp中存的就是基准数
i=left;
j=right;
while (i!=j)
{
while (a[j]>=temp && i<j)//从右边开始找
j--;
while (a[i]<=temp && i<j)//再找左边的
i++;
if (i<j) {
t=a[i];
a[i]=a[j];
a[j]=t;
}//交换两个数在数组中的位置
}
a[left] =a[i];//基准数字归位置
a[i] = temp;
[self quciksort:left and:i-1];//继续处理右边的,递归
[self quciksort:i+1 and:right];//继续处理左边的。递归
}