Merge Sort

1. Brief introduction

Merge sort is an effective and stable sorting algorithm based on the merge operation. This algorithm is a very typical application of divide and conquer (Divide and Conquer). Combine the ordered subsequences to obtain a completely ordered sequence: that is, first make each subsequence in order, and then make the subsequences in order. Merging two sorted lists into one sorted list is called a binary merge. The speed is second only to quick sort, it is used when the memory is small, and it can be used when parallel computing can be performed.

  1. Select two adjacent numbers to form an ordered sequence.
  2. Select two adjacent ordered sequences to form an ordered sequence.
  3. Repeat the second step until all form an ordered sequence.

Second, the principle of the merge sort algorithm

1️⃣ Apply for a space whose size is the sum of the two sorted sequences to store the merged sequence.
2️⃣Set two pointers, the initial positions are respectively the starting positions of the two sorted sequences.
3️⃣ Compare the elements pointed by the two pointers, select the relatively small element and put it into the merge space, and move the pointer to the next position.
4️⃣ Repeat step 3️⃣ until a pointer exceeds the end of the sequence.
5️⃣Copy all the remaining elements of another sequence directly to the end of the merged sequence.

3. Examples

1️⃣Recursive application _

public class MergeSort {
    
    
    public static int[] mergeSort(int[] source, int start, int h) {
    
    
        if (start == h)
            return new int[]{
    
    source[start]};

        int mid = start + (h - start) / 2;
        int[] leftArr = mergeSort(source, start, mid); //左有序数组
        int[] rightArr = mergeSort(source, mid + 1, h); //右有序数组
        int[] target = new int[leftArr.length + rightArr.length]; //新有序数组

        int m = 0, n = 0, s = 0;
        while (m < leftArr.length && n < rightArr.length) {
    
    
            target[s++] = leftArr[m] < rightArr[n] ? leftArr[m++] : rightArr[n++];
        }
        while (m < leftArr.length)
            target[s++] = leftArr[m++];
        while (n < rightArr.length)
            target[s++] = rightArr[n++];
        return target;
    }

    public static void main(String[] args) {
    
    
        int[] source = new int[]{
    
    1, 3, 6, 7, 5, 9, 4, 2, 8, 10};
        int[] target = mergeSort(source, 0, source.length - 1);
        for (int tar : target) {
    
    
            System.out.println(tar);
        }
    }
}

2️⃣ Two-way merge, firstly make the array to be sorted in order. Example of an array sorting method:

  1. Sorting APIs:Arrays.sort(array);
  2. Partial sorting method: Arrays.sort(arr, 2, 6);select the part of the numbers that you want to sort, and put them in the front and not in the back [the sorting part is 2, 3, 4, 5], and the order of other numbers remains unchanged.
  3. bubble sort
  4. selection sort
  5. insertion sort
  6. Quick Sort

Guess you like

Origin blog.csdn.net/ChineseSoftware/article/details/125336806