快速排序

http://blog.sina.com.cn/s/blog_70441c8e0100pxuh.html


void quicksort(int a[], int low, int high)

{

int i = low;

int j = high;

int temp = a[i];  //比较基准值,  如果基准值是a[j]. 那下面的两个循环应该调换位置。

     

         if (low < high) {

while( i < j) {  //每一遍循环的结果是a[i]小于base, a[j]大于base.

while(a[j] >= temp && i < j)

j--;

a[i] = a[j];     //第一次覆盖a[i]是因为a[i]的值已经保存在temp变量里面, 后续循环覆盖是由于a[i]的值已经和基准值temp比较过了.

while(a[i] < temp && i < j)

                               i++;

 a[j] = a[i];  //a[i]的位置已经到a[j]了,所以下一轮循环a[j[可以覆盖a[i]的值。

}

a[i] = temp;  //基准值保存到中间位置

quicksort(a, low, i-1);

quicksort(a, j+1, high);



}

      

    




}

你可能感兴趣的:(快速排序)