快速排序是不稳定的。最理想情况算法时间复杂度O(nlog2n),最坏O(n ^2)。
#include <stdio.h>
void quick_sort(int *a,int len,int left,int right){
int i,j,temp,t;
if(left>right)
return ;
i=left;
j=right;
temp=a[left];
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;
quick_sort(a,i,left,i-1);
quick_sort(a,len-i, i+1, right);
}
int main(int argc, const char * argv[]) {
int a[8]={4,3,2,1,6,8,5,7};
int i;
quick_sort(a,8,0,7);
for (i=0; i<8; i++) {
printf("%d ",*(a+i));
}
return 0;
}