[Code comment details] Java implements merge sorting algorithm

1 Overview

      Merge sort is a stable algorithm that uses the typical application of divide and conquer. Combine existing self-sequences to get a completely ordered sequence. That is, first make each subsequence order, and then make the subsequence order. Then the two ordered lists are combined into one ordered list, which is called two-way merge.

     The merge operation, also called the merge algorithm, refers to the method of merging two sequential sequences into one sequential sequence.

     If there is a number sequence {6,202,100,301,38,8,1}

    Initial state: 6,202,100,301,38,8,1

    After the first merge: {6,202},{100,301},{8,38},{1}

    After the second merge: {6,100,202,301}, {1,8,38}

    After the third merge: {1,6,8,38,100,202,301}

2. Analysis of the idea of ​​merging and sorting algorithm

     ① Apply for a sorted array in the same space as the original array

     ② Define two pointers, the first recognition position is two sorted starting positions

     ③ Compare the elements pointed to by the two pointers, select the smaller one and place it in the new array space, and move the pointer down

    Repeat step ③ until the remaining array is added to the end of the new array

    For example, the initial array: [24,13,26,1,2,27,38,15]

    ① Divided into two sub-arrays of equal size: [24,13,26,1] [2,27,38,15]

    ② It is divided into four sub-arrays of equal size: [24,13] [26,1] [2,27] [38,15]

    ③At this time, left <right is still established, and then divided into: [24] [13] [26] [1] [2] [27] [38] [15]

    At this point, there are 8 small arrays, and each array can be regarded as an ordered array! ! ! , Left == right in each array, return from recursion

        // Separate the array elements
        mergeSort(arr,left,mid);
        mergeSort(arr,mid+1,right);
        // Compare, sort and
        merge the array elements merge(arr,left,mid,right);

       Then——>

       merge([24],[13]) gets [13,24]

       merge([26],[1]) gets [1,26]

3. Code implementation

public class MergeDemo {
	
	public static void main(String[] args) {
		int[] array = {7,3,1,4,8,10};
		mergeSort(array, 0, 5);
		merge(array, 0, 2, 5);
		System.out.println(Arrays.toString(array));
	}
	
	// 一 对整个数组进行排序
	// 先对整个序列进行分组
	// 参数 需要排序的数组  数组的第一个位置  数组的最后一个位置 将排序的数组放在新的数组里面
	private static 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);
	}
	
	// 合并
	private static void merge(int[] arr,int left,int mid,int right) {
		// 定义第一个归并序列开始的地方
		int s1 = left;
		
		// 定义第二个归并序列开始的地方
		int s2 = mid + 1;
		
		// 定义已经归并好的序列元素存放的位子
		int[] temp = new int[arr.length];
		//定义临时序列的下标 从0开始
		int i = 0;
		// 向临时序列赋值
		while(s1 <= mid && s2 <= right) {
			if(arr[s1] <= arr[s2]) {
				temp[i++] = arr[s1++];
			}else {
				temp[i++] = arr[s2++];
			}
		}
		
		// 如果其中一个序列比较完了 剩下的那组序列就放进去
		while(s1 <= mid) {
			temp[i++] = arr[s1++];
		}
		while(s2 <= right) {
			temp[i++] = arr[s2++];
		}
		// 将temp中的元素 复制到 arr序列中
		i = 0;
		while(left <= right) {
			arr[left++] = temp[i++];
		}
	}
}

4. Algorithm complexity analysis

    The time complexity of merge sort is O (nlogn). For specific calculations, please visit: https://blog.csdn.net/a1033025319/article/details/88650514

   Merge sort is the least number of comparisons among all sorts. It is divided continuously at the beginning, and the comparison only occurs in the merged ordered sub-arrays.

5. Summary

   The more difficult to understand recursive method of merge sort is sort merge. At first, I was confused. How to sort the separate parts of the array? Call this line of code: merge(arr,left,mid,right). Sort the elements when merging them.

   OK, that's it for today, if you like it, please give a thumbs up.

 

 

Guess you like

Origin blog.csdn.net/Sunshineoe/article/details/114747590