[算法](00004) 快速排序

2018-07-17  本文已影响9人  krmao
public class QuickSort {

    private static void quickSort(final int array[], final int originLeft, final int originRight) {

        if (originLeft < originRight) {

            int baseValue = array[originLeft];
            int tmpLeft = originLeft;
            int tmpRight = originRight;

            while (tmpLeft != tmpRight) {

                while (tmpLeft < tmpRight && array[tmpRight] >= baseValue) tmpRight--;
                array[tmpLeft] = array[tmpRight];

                while (tmpLeft < tmpRight && array[tmpLeft] <= baseValue) tmpLeft++;
                array[tmpRight] = array[tmpLeft];

            }

            array[tmpRight] = baseValue;
            quickSort(array, originLeft, tmpLeft - 1);
            quickSort(array, tmpRight + 1, originRight);

        }

    }

    public static void main(String[] args) {

        int array[] = {10, 5, 4, 1, 5, 2, 7, 3, 0, 4, 9, 6, 8};

        System.out.println("排序之前:");
        for (int element : array) System.out.print(element + " ");

        quickSort(array, 0, array.length - 1);

        System.out.println("\n排序之后:");
        for (int element : array) System.out.print(element + " ");

    }
}

参考

上一篇 下一篇

猜你喜欢

热点阅读