Vernacular resolve merge sort

    In simple terms, merge sort mainly to the more ordered sequences into an ordered sequence.

    We first look at two simple numbers 5, 2   , 5 and 2 will then direct comparison of exchange becomes position 2,5  . Then add two numbers 4,3  , Similarly, the comparison becomes exchange  3,4

    We will now 2,5  and  3,4 these two sequences into an ordered sequence, there is no basis for the algorithm of the students first thought might be to find the smallest number, then the second small number of three small number etc., and finally turn it into a new array to complete.

    But today we are discussing an alternative, we first two series of the first number 2 and 3 compare selected smaller 2 into the new array, and then move one after the first sequence subscript 3 and 5 continue to compare selected smaller 3 into the new array, and then the second shift after a sequence subscript 4 and 5 continue to compare selected smaller 4 into the new array, and finally 5 into the new array, the data on this new array is 2,3,4,5 ordered sequence. This is called merge sort.

Now we look at merge sort code:

public static void mergeSort(int arr[],int left,int right){
        if(left>=right)return;

        int mid=(right+left)/2;

        mergeSort(arr,left,mid);
        mergeSort(arr,mid+1,right);

        merge(arr,left,mid,right);

    }

    private static void merge(int[] arr, int left, int mid, int right) {

        int tmp[]=new int[right-left+1];
        int leftIndex=left;
        int rightIndex=mid+1;
        int tmpIndex=0;

        while (leftIndex<=mid && rightIndex<=right){
            if(arr[leftIndex]<=arr[rightIndex]){
                tmp[tmpIndex++]=arr[leftIndex++];
            }else{
                tmp[tmpIndex++]=arr[rightIndex++];
            }
        }

        int start=leftIndex,end=mid;

        if(rightIndex<=right){
            start=rightIndex;
            end=right;
        }

        while (start<=end){
            tmp[tmpIndex++]=arr[start++];
        }

        for(leftIndex=0;leftIndex<=right-left;leftIndex++){
            arr[left+leftIndex]=tmp[leftIndex];
        }
    }

    Subscripts left border and right border subscript mergeSort method for the sorted array arr, left and right, respectively arr array, this is by way of recursive partition of the array, and then the operation of merge sort. That is half the array constantly, until the left margin greater than or equal right boundary can not be half so far.

  Now we look at the array 5, 3, 2, 6, 4, a merge process is performed, wherein the combining is performed referring to merge method:

                                                 5 3 2 6 4 1 left = 0 right = 5
                    first recursive 5 3 2 left = 0 right = 2
                    second recursive 5 3 left = 0 right = 1
                    Third recursive 5 left = 0 right = 0 left > = right, return termination condition is reached
                    the fourth recursive 3 left = 1 right = 1 left > = right, return termination condition is reached
                    for the first time combined            . 3. 5 2. 6. 1. 4 left = right = 0 = 0. 1 MID
                    fifth recursive 2 left = 2 right = 2 left > = right, return termination condition is reached
                    the second combined            2356 4 1 left = 0 mid = 1 right = 2
                    Sixth recursive 6 4 1 left = 3 right = 5
                    Sixth recursive 6 4 left = 3 right = 4
                    Sixth recursive 6 left = 3 right = 3 left > = right, return termination condition is reached
                    seventh recursive 4 left = 4 right = 4 left > = right, return termination condition is reached
                    the third combined. 5. 3 2 . 4. 6 . 1 = left = right. 3. 4. 3 = MID
                    eighth recursive 1 left = 5 right = 5 left > = right, return termination condition is reached
                    combined fourth. 3. 5 2 . 1. 4. 6                 left = 3 mid = 4 right = 5
                    fifth combined            . 1 2. 5. 4. 3. 6                  left = right = 0 MID = 2. 5

 

    The above is a detailed execution procedure of a merge sort, marked red data exchange occurs in the merging process.

    Now let's look at a detailed analysis merge method to merge last time the above example, we look at the implementation process of merge:

     First, create a tmp array for storing data has been sorted. Then mid into left and right sub-array 2, respectively, with the center  leftIndex and rightIndex for traversing two subscripts, the smaller number is selected, its corresponding index plus 1, ad infinitum, until traversed to the end point. Finally, the remaining data sub-array into an array tmp, tmp the data copied into the can in the original array arr.

 

Merge optimization:

    For merge method, there is a way to optimize:

private static void mergeGuard(int[] arr, int left, int mid, int right) {
        int[] leftArr = new int[mid - left + 2];
        int[] rightArr = new int[right - mid + 1];

        for (int i = 0; i <= mid - left; i++) {
            leftArr[i] = arr[left + i];
        }
        // 第一个数组添加哨兵(最大值)
        leftArr[mid - left + 1] = Integer.MAX_VALUE;

        for (int i = 0; i < right - mid; i++) {
            rightArr[i] = arr[mid + 1 + i];
        }
        // 第二个数组添加哨兵(最大值)
        rightArr[right-mid] = Integer.MAX_VALUE;

        int i = 0;
        int j = 0;
        int k = left;
        while (k <= right) {
            // 当左边数组到达哨兵值时,i不再增加,直到右边数组读取完剩余值,同理右边数组也一样
            if (leftArr[i] <= rightArr[j]) {
                arr[k++] = leftArr[i++];
            } else {
                arr[k++] = rightArr[j++];
            }
        }
    }

   The main store is the maximum boundary of an array of integers or so, so that when the merge, you can be around all of the data into the array arr are not needed after the completion of the comparison data, additional data will put border into the new array.

 

to sum up:

    Merge sort, essentially by way of the partition, a bit sequence is split into a number, finally merging. But the human mind is positive, it will be combined into a number of easy to understand when an ordered sequence, reverse sequence will then split merge it a little more difficult to understand. This article first use split mode with everyone understand the core merge, and then take you step by step to go in-depth understanding of merge sort.

Published 25 original articles · won praise 51 · views 20000 +

Guess you like

Origin blog.csdn.net/Royal_lr/article/details/102957074