int partition(int A[] , int len)
{
int pivot = A[0];
int low = 0;
int high = len-1;
while(low < high){
for(; high>low && A[high] >= pivot; high--);
A[low] = A[high];
for(; high>low && A[low] <= pivot; low++);
A[high] = A[low];
}
A[low] = pivot;
return low;
}
void quicksort(int A[], int len)
{
if(len == 0 || len == 1)
return;
int index = partition(A,len);
quicksort(A, index);
quicksort(A+index+1, len-index-1);
}
int main()
{
int A[] = {1,5,4,7,9,2,3,6,8};
int len = sizeof(A)/sizeof(A[0]);
for(int i = 0; i < len ; i++)
printf("%d ", A[i]);
printf("\n");
quicksort(A,len);
for(int i = 0; i < len ; i++)
printf("%d ", A[i]);
printf("\n");
return 1;
}
网友评论