Sorting algorithm merge sort (Java version)

Merge sort of sorting algorithm

Merge sort is one of the applications of divide and conquer strategy, divide and conquer.
The time complexity is O(n log n)
. The idea of ​​merge sorting is: split the queue into sub-queues until it is indivisible, and then merge the sub-sequences in order to obtain a completely ordered sequence; that is, first make each sub-sequence have Order, and then make the sub-sequence segments orderly.

core

The core of merge sort is 分治思想and递归实现

  • Divide and conquer strategy: decompose the original problem into several smaller but similar sub-problems, solve these sub-problems recursively, and then merge the solutions of the sub-problems to establish the solution of the original problem.

No picture, no truth, merge sort flow chart (pirate Baidu Baike?)
Insert picture description here

Analysis of Algorithms

  • First decompose the queue recursively (using midpoint decomposition) until the number of elements in the queue is 1
  • The decomposition queue is divided into left queue (low, mid) and right queue (mid+1, high) by calculating the midpoint of the queue
  • When the original queue is split into the smallest queue (there is only one element in the queue), the sub-queue is ordered
  • Combine two ordered queues into one ordered queue, the mergeArraymethod in the code

Code

The comments are more detailed, so I won’t go into details


    /**
     * 
     * @param array 排序数组
     * @param start 开始位置
     * @param end 结束位置
     */
    private void mergeSort(int[] array, int start, int end) {
    
    
        if (start < end) {
    
    //当数组中的元素不可分时,停止分解
            int mid = (start + end) / 2;//计算中间坐标
            mergeSort(array, start, mid);//递归分解数组
            mergeSort(array, mid + 1, end);//递归分解数组
            //合并数组,合并后为有序数组
            //因为前面为递归调用,所以可以保证第一次调用此方法时,子数组长度为1
            mergeArray(array, start, mid, end);
        }
    }
    
    private void mergeArray(int[] array, int start, int mid, int end) {
    
    
        int[] left = new int[mid - start + 1 //存放数据长度
                + 1];//预留安全守卫
        int[] right = new int[end - mid//存放数据长度
                + 1];//预留安全守卫
        System.arraycopy(array, start, left, 0, left.length - 1);
        System.arraycopy(array, mid + 1, right, 0, right.length - 1);

        left[left.length - 1] = SAFE_GUARD;
        right[right.length - 1] = SAFE_GUARD;
        
        int l = 0, r = 0;

        for (int i = start; i <= end; i++) {
    
    
            if (right[r] == SAFE_GUARD) {
    
    
                array[i] = left[l];
            }
            if (left[l] == SAFE_GUARD) {
    
    
                array[i] = right[r];
            }
            if (left[l] <= right[r]) {
    
    
                array[i] = left[l];
                l++;
            } else {
    
    
                array[i] = right[r];
                r++;
            }
        }
    }

Conclusion

I personally think that the important thing about merging and sorting is the idea of ​​divide and conquer. JDK7Introduced applications that ForkJoinPoolare also divide and conquer strategies. In that case, you need to understand the divide and conquer strategy!
The second day of "Introduction to Algorithms", come on! Next分治策略

Guess you like

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