复习之快速排序

原理转载自http://www.cnblogs.com/yylogo/archive/2011/06/09/qsort.html,讲的非常透彻,但代码没有完全按他的思路编写


package com.lyj.sort;

public class QuickSort {
    private static int[] array = { 45 ,   36  ,  18 ,   53   , 72 ,   30  ,  48  ,  93  ,  15 ,    36};

    public static void main(String[] args) {
        quickSort(0, array.length - 1);

        for (int value : array) {
            System.out.print(value + "  ");
        }
    }

    private static void quickSort(int begin, int end) {
        if (begin < end) {
            //起始游标
            int i = begin;
            //结束游标,加1是因为后面--j
            int j = end + 1;
            
            // array[begin]是pivot
            while(true) {
                while(array[++i] < array[begin] && i < end){}  
                while(array[--j] > array[begin]){}
                
                if(i < j) {
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                } else {
                    break;
                }
            }
            
            //pivot和j交换
            int temp = array[j];
            array[j] = array[begin];
            array[begin] = temp;
            
            quickSort(begin, j - 1);
            quickSort(j + 1, end);
        }
    }
}



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