Java programming: sorting algorithm-merge sort

Introduction to Merge Sort

Merge sorting (MERGE-SORT) is a sorting method implemented using the idea of ​​merging. The algorithm uses the classic divide-and-conquer strategy (divide-and-conquer) to divide the problem into small problems and then recursively solve , And the conquer stage "fixes" the answers obtained in the different stages together, that is, divide and conquer).

Schematic diagram of merge sort idea 1-basic idea

Insert picture description here

Description:

It can be seen that this structure is very similar to a complete binary tree. The merge sort in this article is implemented recursively (or iteratively). Stages can be understood as the process of recursively splitting subsequences.

Schematic diagram of merge sort idea 2- merge adjacent ordered subsequences:

Let's take a look at the treatment stage. We need to merge two already ordered subsequences into an ordered sequence. For example, in the last merging in the above figure, [4,5,7,8] and [1,2, 3,6] Two already ordered subsequences are merged into the final sequence [1,2,3,4,5,6,7,8], let’s look at the implementation steps
Insert picture description here

Application examples of merge sort

Give you an array, val arr = Array(9,8,7,6,5,4,3,2,1), please use merge sort to complete the sort.

Code

package sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class MergeSort {
    
    
    public static void main(String[] args) {
    
    
//        int[] arr = {8, 4, 5, 7, 1, 3, 6, 2};
//        int[] temp = new int[arr.length];   // 归并排序有一个额外的空间
//        mergeSort(arr, 0, arr.length - 1, temp);
//        System.out.println(Arrays.toString(arr));

        // 测速
        int[] arr = new int[80000000];
        for (int i = 0; i < 80000000; i++) {
    
    
            arr[i] = (int) (Math.random() * 80000);// 生成一个0-80000的数据
        }
        int[] temp = new int[arr.length];
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1Str = simpleDateFormat.format(date1);
        System.out.println("排序前的时间为:" + date1Str);
        mergeSort(arr,0,arr.length-1,temp);
        Date date2 = new Date();
        String date2Str = simpleDateFormat.format(date2);
        System.out.println("排序后的时间为:" + date2Str);
    }

    // 分+合方法
    public static void mergeSort(int[] arr, int left, int right, int[] temp) {
    
    
        if (left < right) {
    
    
            int mid = (left + right) / 2;
            // 向左递归进行分解
            mergeSort(arr, left, mid, temp);
            // 向右递归进行分解
            mergeSort(arr, mid + 1, right, temp);
            // 到合并
            merge(arr, left, mid, right, temp);
        }
    }


    // 合并的方法

    /**
     * @param arr   待排序原始数组
     * @param left  左边有序序列初始索引
     * @param mid   中间索引
     * @param right 右边索引
     * @param temp  中转数组
     */
    public static void merge(int[] arr, int left, int mid, int right, int[] temp) {
    
    
        int i = left;   // 初始化i,左边有序序列的初始索引
        int j = mid + 1;// 初始化j,右边有序序列的初始索引
        int t = 0;  // 指向temp数组的当前索引

        // (一)先把左右两边的数据按规则填充到temp数组,直到左右两边有序序列有一边处理完毕为止
        while (i <= mid && j <= right) {
    
    
            // 如果左边的有序序列的当前元素小于等于右边有序序列的当前元素
            // 即将当前的左边元素,拷贝到temp数组
            // t和i后移
            if (arr[i] <= arr[j]) {
    
    
                temp[t++] = arr[i++];
            } else {
    
     // 反之将右边的当前数据填充到temp数组
                temp[t++] = arr[j++];
            }
        }
        // (二)把有剩余数据的一边的剩余数据依次全部填充到temp
        while (i <= mid) {
    
      // 左边的有序序列仍有剩余,将其全部填充到temp
            temp[t++] = arr[i++];
        }
        while (j <= right) {
    
    // 右边的有序序列仍有剩余,将其全部填充到temp
            temp[t++] = arr[j++];
        }
        // (三)将temp数组的元素拷贝到arr
        // 注意,并不是每次都拷贝所有
        t = 0;
        int tempLeft = left;
        while (tempLeft <= right) {
    
    
            arr[tempLeft++] = temp[t++];
        }
    }

}

Guess you like

Origin blog.csdn.net/KaiSarH/article/details/108867554