#include <stdio.h>
#include <stdlib.h>
int main(){
int a[16]={12,342,3,343,43,3,9,33,34,10,2,5,7,1,6,3};
int *p= (int * )malloc(100*sizeof(int));
int index=0;
p[0] = 0;
p[1] = 16;
index+=2;
while(index>0){
int left=*(p+index-2);
int right=*(p+index-1);
index-=2;
int key=a[left];
int srcLeft=left;
int srcRight=right;
while(left<right){
while(left<right && a[right]>=key){
right--;
}
a[left]=a[right];
while(left<right && a[left]<=key){
left++;
}
a[right]=a[left];
a[left]=key;
}
if(left-srcLeft>=2){
printf("左边继续排序,起始下标%d,结束下标%d\n",srcLeft,left);
*(p+index++)=srcLeft;
*(p+index++)=left-1;
}
if(srcRight-right>=2){
printf("右边继续排序,起始下标%d,结束下标%d\n",right,srcRight);
*(p+index++)=right+1;
*(p+index++)=srcRight;
}
}
for(int i=0;i<16;i++){
printf("%d\n",a[i]);
}
free(p);
return 0;
}
gcc -std=c99 -g -o quicksort quicksort.c && ./quicksort