Sorting algorithm --- merge algorithm (java version)

Merge sort

principle

The core idea of ​​Merge Sort (Merge Sort) is that if we want to sort an array, we first divide the array into two parts from the middle, and then sort the two parts separately, and then merge the sorted two parts together, so that the whole The arrays are all ordered. The merge sort uses the divide and conquer idea.

Divide and conquer

Divide and conquer, as the name implies, is to divide and conquer, to resolve a big problem into smaller sub-problems. When the small sub-problems are solved, the big problems are solved. Divide and conquer ideas are generally implemented by recursion. Divide and conquer is a processing idea to solve problems, and recursion is a programming technique, the two do not conflict. For recursion, it is necessary to find the recursion formula and termination conditions, so write the recursive formula mergeSort(m->n) = merge(mergeSort(m->k), mergeSort(k+1->n)) ; Terminate when m=n. That is, when we want to sort the sequence between m->n, we can actually divide it into sorting the sequence between m->k, and sort the sequence between k+1->n, and then connect The merging of the arranged series is called the final series. In the same way, the sorting of each series can continue to be split down to form recursion.

The algorithm process is as follows

Insert picture description here

Algorithm Description:

  • Divide the input sequence of length n into two subsequences of length n/2;
  • Use merge sorting for these two subsequences respectively;
  • Finally, the two sorted subsequences are merged into a final sorted sequence.

Code

public class MergeSort {
    
    
    //1.如果要排序一个数组,我们先把数组从中间分成前后两部分,然后对前后两部分分别排序
    //2.再将排好序的两部分合并在一起,这样整个数组就都有序了

    public static void main(String[] args) {
    
    
        int[] array = {
    
    5, 2, 6, 9, 0, 3, 11, 7};
        MergeSort sort = new MergeSort();
        int[] ints = sort.mergeSort(array);
        System.out.println(Arrays.toString(ints));

    }

    public int[] mergeSort(int[] arr) {
    
    
        if (arr.length < 2) {
    
    
            return arr;
        }
        //将数组从中间拆分成左右两部分
        int mid = arr.length / 2;
        int[] left = Arrays.copyOfRange(arr, 0, mid);
        int[] right = Arrays.copyOfRange(arr, mid, arr.length);
        return merge(mergeSort(left), mergeSort(right));

    }

    //合并两个有序数组并返回新的数组

    public int[] merge(int[] left, int[] right) {
    
    
        //创建一个新的数组,等于left+right之和
        int[] newArray = new int[left.length + right.length];

        //定义两个指针,分别代表两个数组的下标
        int lindex = 0;
        int rindex = 0;
        for (int i = 0; i < newArray.length; i++) {
    
    
            if(lindex >= left.length){
    
    
                newArray[i] = right[rindex++];
            }else if(rindex >= right.length){
    
    
                newArray[i] = left[lindex++];
            }else if(left[lindex] < right[rindex] ){
    
    
                newArray[i] = left[lindex++];
            }else{
    
    
                newArray[i] = right[rindex++];
            }
        }
        return newArray;
    }
}

1: What is the time complexity of merge sort?

The execution efficiency of merge sort has nothing to do with the order of the original array to be sorted, so its time complexity is very stable, regardless of the best case, worst case, or average case, the time complexity is O(nlogn) .

2: What is the space complexity of merge sort ?

The space complexity of merge sort is O(n).

3: Is merge sort a stable sorting algorithm?

The stability of the merge sort algorithm does not depend on the merge function merge(). That is, the part of the code where two ordered sub-arrays are merged into an ordered array. By analyzing the merge function, we find that merge sort is also a stable sorting algorithm.

Guess you like

Origin blog.csdn.net/qq_33626996/article/details/113177860