快速排序

/**
* 开始排序
* @author Administrator
*
*/
public class QuickSort {



int get(int[] a, int low, int high){
    int compare = a[low];

     while(low < high){ //无论如何置换, 被置换的都包含compare的值
         while(low<high && a[high]>=compare)   
              high--;

         //在 low<high 的情况下找到a[high]<compare并置换
         int  temp = a[low];
         a[low] = a[high];
         a[high] = temp;

         while(low<high && a[low]<=compare)
              low++;

         //在 low<high 的情况下找到a[low]>compare并置换
          temp = a[low];
          a[low] = a[high];
          a[high] = temp;
     }
     return low; //while(low==hight)停止循环, 并返回枢轴位置
}

void quickSort(int[] a, int low, int high) {
if (low < high) {
int p = getMiddle(a, low, high);
        quickSort(a, low, p-1);
        quickSort(a, p+1, high);
}
}

public int getMiddle(int[] list, int low, int high) { 
    int tmp = list[low];    //数组的第一个作为中轴 
    while (low < high) { 
        while (low < high && list[high] > tmp) { 
            high--; 
        } 
        list[low] = list[high];   //比中轴小的记录移到低端 
        while (low < high && list[low] < tmp) { 
            low++; 
        } 
        list[high] = list[low];   //比中轴大的记录移到高端 
    } 
    list[low] = tmp;              //中轴记录到尾 
    return low;                   //返回中轴的位置 

/**
* @param args
*/
public static void main(String[] args) {
QuickSort quickSort=new QuickSort();
int[] a={3,2,6,9,7,1,4,5};
quickSort.quickSort(a,0,a.length-1);

for(int i:a){
System.out.println(i);
}

}

}

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