Merge Sort by Classic Algorithm

insert image description here

Event address: CSDN 21-day Learning Challenge

merge sort

Merge sort is mainly implemented in two parts, divided and combined. Divide is to divide the array into two halves, and then recursively divide the subarrays until they are divided into separate numbers. Merge is to merge two arrays into an ordered array, and then merge the ordered arrays until all sub-arrays are merged into a complete array.

Algorithm principle

  • Allocate space so that its size is the sum of the two sorted sequences, and this space is used to store the merged sequence

  • Set two pointers, the initial positions are the starting positions of the two sorted sequences

  • Compare the elements pointed to by the two pointers, select a relatively small element to put into the merge space, and move the pointer to the next position

  • Repeat step c until a pointer exceeds the end of the sequence

  • Copy all remaining elements of another sequence directly to the end of the merged sequence

Animated demo

insert image description here

Code

public class MergeSort {
    
    
    //归并所需的辅助数组
    private static Comparable[] assist;//比较 v 是否小于 w
    public static boolean less(Comparable v,Comparable w){
    
    
        return v.compareTo(w) < 0;
    }//数组元素交换位置
    private static void swap(Comparable[] a,int i,int j){
    
    
        Comparable temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    //排序
    public static void sort(Comparable[] a){
    
    
        //初始化辅助数组
        assist = new Comparable[a.length];
        int l = 0;
        int h = a.length - 1;
        sort(a,l,h);
    }private static void sort(Comparable[] a,int l,int h){
    
    
        if (h <= l){
    
    
            return;
        }
        //分组
        int mid = l +(h - l) / 2;
        //分别对每组数据排序
        sort(a,l,mid);
        sort(a,mid + 1,h);
        //对数组进行归并
        merge(a,l,mid,h);
    }//对数组进行归并
    private static void merge(Comparable[] a,int l,int mid,int h){
    
    
        //定义三个指针
        int i = l;
        int p1 = l;
        int p2 = mid + 1;
        //遍历,移动p1,p2指针,比较两处索引的值,小的放到辅助数组的对应索引处
        while (p1 <= mid && p2 <=h){
    
    
            if (less(a[p1],a[p2])){
    
    
                assist[i++] = a[p1++];
            }else {
    
    
                assist[i++] = a[p2++];
            }
        }
        //遍历数组,如果p1的指针没有走完,则顺序移动p1指针,把对应的元素放到辅助数组的对应索引处
        while (p1 <= mid){
    
    
            assist[i++] = a[p1++];
        }
        //遍历数组,如果p2的指针没有走完,则顺序移动p2指针,把对应的元素放到辅助数组的对应索引处
        while (p2 <= h){
    
    
            assist[i++] = a[p2++];
        }
        //把辅助数组中的元素拷贝到原数组中
        for (int j = l; j <= h; j++) {
    
    
            a[j] = assist[j];
        }
    }
}
public class MergeSortTest {
    
    
    public static void main(String[] args) {
    
    
        Integer[] arr = {
    
    5,6,3,1,8,7,2,4};
        MergeSort.sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
//排序前:{5,6,3,1,8,7,2,4}
//排序后:{1,2,3,4,5,6,7,8}

the complexity

  • Time complexity: O(nlogn)

  • Space Complexity: O(n)

Guess you like

Origin blog.csdn.net/weixin_52986315/article/details/126448104