[算法] 快速排序
参考 http://www.cnblogs.com/morewindows/archive/2011/08/13/2137415.html
http://baike.baidu.com/view/19016.htm?fromId=115472
#define MAXLENGTH 10
void quick_sort(int a[], int start, int end)
{
if(start<end){
int key=a[start];//开始比较对象
int i=start;
int j=end;
while (i<j) {
while (i<j &&a[j]>=key) {
j--; //开始比较右边,直到发现小于key的数
}
if(i<j){
a[i]=a[j];
a[j]=key; //交换
i++;
}
while (i<j && a[i]<key) {
i++;
}
if(i<j){
a[j]=a[i];
a[i]=key;
j--;
}
}
a[i]=key;
quick_sort(a,start,i-1);//左边
quick_sort(a, i+1, end);//右边
}
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
int a[MAXLENGTH]={9,56,45,76,23,45,37,12,19,3};
for (int b=0; b<MAXLENGTH ; b++) {
printf("%3d",a[b]);
}
quick_sort(a,0,MAXLENGTH-1);//起始位置,结束位置
printf("\n");
for (int b=0; b<MAXLENGTH; b++) {
printf("%3d",a[b]);
}
}
return 0;
}