美文网首页
[算法](00004) 快速排序

[算法](00004) 快速排序

作者: krmao | 来源:发表于2018-07-17 10:47 被阅读9次
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 + " ");

    }
}

参考

相关文章

网友评论

      本文标题:[算法](00004) 快速排序

      本文链接:https://www.haomeiwen.com/subject/idjwpftx.html