【数据结构与算法(Java)】快速排序(图文解析)

1. 实现思路

1.1 算法方法结构

1. void quickSort(int[] array, int left, int right) 
2. int[] partition(int[] array, int left, int right)
3. void swap(int[] array, int index1, int index2)

1.2 图解思路

step1+2
step3+3.1
step3.2(1)
step3.2(2)
step3.2(3)
step3.2(loop)
step3.3
step3.4+step4

2. 代码实现

/**
     * quick sort method for array
     * @param array - original array
     * @param left - left edge index
     * @param right - right edge index
     */
    public static void quickSortZ(int[] array, int left, int right) {
        // step 1:
        // when left is equal to or more than right, stop recursion
        if (left < right) {
            // step 2:
            // find a random position between left and right, swap it with right
            swapZ(array, right, left + (int) (Math.random() * (right - left + 1)));
            // step 3:
            // put all numbers as "less, equal, right", and return the edges of "equal"
            int[] edges = partitionZ(array, left, right);
            // step 4:
            // recurse the left part of the quickSortZ()
            quickSortZ(array, left, --edges[0]);
            // recurse the right part of the quickSortZ()
            quickSortZ(array, ++edges[1], right);
        }
    }

    /**
     * divide the part(between left and right) of the array into three parts:
     *      1. less than pivot part
     *      2. equal to pivot part
     *      3. more than pivot part
     * @param array - original array
     * @param left - left edge index
     * @param right - right edge index
     * @return - an int array with 2 items for recording the edge index of the "equal to pivot part"
     *           array[0] = left edge index of the "equal to pivot part"
     *           array[1] = right edge index of the "equal to pivot part"
     */
    private static int[] partitionZ(int[] array, int left, int right) {
        // step 3.1:
        // initialize variables for the index of less edge and right edge
        int lessEdge = left - 1;
        int moreEdge = right;
        // step 3.2:
        // stop the loop when left index is equal to or more than more edge index
        while (left < moreEdge) {
            // (1) if current index(left) is less than the pivot(right), put it to "less zone"
            if (array[left] < array[right]) {
                swapZ(array, left++, ++lessEdge);
            }
            // (2) if more than it, put to "more zone"
            else if (array[left] > array[right]) {
                swapZ(array, left, --moreEdge);
            }
            // (3) if equals, put to the middle zone
            else {
                left++;
            }
        }
        // step 3.3:
        // swap the pivot(right) and the more edge
        swapZ(array, right, moreEdge);
        // step 3.4:
        return new int[]{++lessEdge, moreEdge};
    }

    /**
     * swap the two items in the array
     * @param array - original array
     * @param index1 - a index of the item
     * @param index2 - a index of the item
     */
    private static void swapZ(int[] array, int index1, int index2) {
        int temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值