Merge Sort of Data Structures and Algorithms

Overview        

        Merge sort (MERGE-SORT) is an efficient sorting algorithm based on the merge operation, which is a very typical application of divide and conquer method (Divide and Conquer). Merge the ordered subsequences to obtain a completely ordered sequence; that is, first make each subsequence ordered, and then make the subsequence segments ordered. If two sorted lists are merged into one sorted list, it is called two-way merge.


Thought        

        The merging process is: compare the size of a[i] and b[j], if a[i]≤b[j], then copy the element a[i] in the first ordered list to r[k] , and add 1 to i and k respectively; otherwise, copy the element b[j] in the second ordered list to r[k], and add 1 to j and k respectively, and so on, until the After one sorted list is taken, the remaining elements in the other sorted list are copied to the cells from subscript k to subscript t in r. The algorithm of merge sort is usually implemented recursively. First, the interval to be sorted [s, t] is divided by the midpoint, then the left sub-interval is sorted, then the right sub-interval is sorted, and finally the left and right intervals are merged with one operation. Merge into ordered intervals [s,t].

Process demonstration

Let's take a look at the schematic diagram of the merge sort process:


It can be clearly seen from the figure that the idea of ​​merging is to divide a large amount of data into many groups of data, and then sort them by each group. and then merge them. It is not difficult to come up with the code from this:

public class MergeSort {
    /**
     *   @author:kevin
     * @Description: merge sort
     *   @Date:22:25 2018/4/10
     */
    public void sort(int[] arr, int n){
        mergeSort(arr, 0, n-1);
    }
    /**
     *   @author:kevin
     * @Description: recursive call
     *   @Date:22:25 2018/4/10
     */
    public void mergeSort(int[] arr, int left, int right){
        if (left >= right){
            return;
        }
        int mid = (left + right) / 2;
        mergeSort(arr, left, mid);
        mergeSort(arr, mid+1, right);
        merge(arr, left, mid, right);
    }
    /**
     *   @author:kevin
     * @Description: Merge process
     *   @Date:22:25 2018/4/10
     */
    public void merge(int[] arr, int left, int mid, int right){
        int[] tempArr = new int[right-left+1];
        int k = 0;
        int i = left;
        int j = mid+1;
        while (i <= mid && j <= right){
            if (arr[i] < arr[j]){
                tempArr[k++] = arr[i++];
            }
            if (arr[i] >= arr[j]){
                tempArr[k++] = arr[j++];
            }
        }
        while (i <= mid){
            tempArr[k++] = arr[i++];
        }
        while (j <= right){
            tempArr[k++] = arr[j++];
        }
        for (int l = 0; l < tempArr.length; l++) {
            arr[l+left] = tempArr[l];
        }

    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325543301&siteId=291194637