Sorting algorithm of quick sort (Java version)

Quick Sort is a worst-case time complexity of o(n^2)the algorithm. Although the worst-case time complexity is poor, quick sort is usually the best choice in actual sorting applications because its average performance is very good: its expected time sorting complexity iso(nlgn)

description

Like merge sort, the quick sort algorithm also uses the divide and conquer idea. The following are the three divide and conquer processes of quick sort:

  • Decomposition: Array partition, here we take the last element as the partition standard, and the partition logic is shown in the figure below:
    Insert picture description here

  • Solution: Recursively call quick sort to sort the left and right arrays.

  • Merging: Because the sub-arrays are all sorted in-situ, there is no need to merge.

achieve

Array partition implementation: use the last element as the sample element to partition. Divide the array into three groups: left, middle (sample element storage area), and right. The left area is smaller than the middle, the right area is larger than the middle

    /**
     * 数组分区算法
     * 以最后一个元素为样本元素,进行分区
     * 将数组划分为左、中(样本元素存放区域)、右三组。左侧区域小于中,右侧区域大于中
     *
     * @param array 待排序数组
     * @param start 数组开始位置
     * @param end   数组结束位置
     * @return 样本元素位置
     */
    private static int partition(int[] array, int start, int end) {
    
    
        int sample = array[end];
        //默认左侧区域为空
        int leftEnd = start - 1;
        int temp;

        for (int j = start; j < end; j++) {
    
    
            //如果当前位置元素值小于样本值
            if (array[j] <= sample) {
    
    
                //将当前元素移动到左侧区域,即左侧区域空间增加1
                leftEnd++;
                temp = array[leftEnd];
                array[leftEnd] = array[j];
                array[j] = temp;
            }
        }
        //将样本元素移动到中间区域,也就是与左侧区域紧挨着
        //leftEnd + 1 就是样本元素所在位置
        //如此形成左、中、右的区间排布
        temp = array[leftEnd + 1];
        array[leftEnd + 1] = array[end];
        array[end] = temp;

        //输出分区后的数组
        System.out.println(Arrays.toString(array));
        return leftEnd + 1;
    }

Sorting implementation: It is not much different from other recursive calls, just pay attention to removing sample elements every time you sort a sub-array.

   /**
     * 快速排序
     * @param array 排序数组
     * @param start 数组起始位置
     * @param end 数组结束位置
     */
    private static void quickSort(int[] array, int start, int end) {
    
    
        if (start < end){
    
    
            //对数组进行分区,记录分区后的样本元素位置点位置
            int mid = partition(array,start,end);
            //对样本元素左侧区域进行递归排序
            quickSort(array,start,mid-1);
            //对样本元素右侧区域进行递归排序
            quickSort(array,mid+1,end);
        }
    }

This article was four days later than expected, and it was produced with "free time" from the busy schedule. The quality is average and the price is insistent! Everyone, come on, the next article "Linear Time Sorting Algorithm"

Guess you like

Origin blog.csdn.net/lijie2664989/article/details/84963752