Sort algorithm (V) - Merging Sort

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/A1344714150/article/details/88699902

Objective: merge sort method sorts the random array

Merge sort idea : Suppose array has the number n, which is first divided into two halves, the two halves then continued for half partition ... repeated until only a small portion of the rear of a partition number. Then these two numbers and merge operation, the ordered merge to give a small array composed of two numbers, which is smaller then the other ordered arrays of numbers consisting of two merging is repeated until all array orderly.

The reason why the first ever separated to the left and right sides of the interior to ensure an orderly, you can merge operations.

If not long-winded, or directly on the bar code:

	//归并排序法
	public static void mergeSort(int[] array){
		__mergeSort(array,0,array.length-1);
	}
	
	
	private static void __mergeSort(int[] array, int l, int r) {
		if(l>=r){
			return ;
		}
		
		int mid = (r-l)/2 + l;
		__mergeSort(array,l,mid);
		__mergeSort(array,mid+1,r);
		__merge(array,l,r,mid);
	}
	
	private static void __merge(int[] array,int l,int r,int mid){
		int[] temp = new int[r-l+1];
		for(int i=l;i<=r;i++){
			temp[i-l] = array[i]; 
		}
		
		int i = l;
		int j = mid+1;
		
		for(int k=l;k<=r;k++){
			if(i>mid){
				array[k] = temp[j-l];
				j++;
			}else if(j>r){
				array[k] = temp[i-l];
				i++;
			}else if(temp[i-l]<temp[j-l]){
				array[k] = temp[i-l];
				i++;
			}else{
				array[k] = temp[j-l];
				j++;
			}
		}
		
	}

Sort results:

The basic version can then merge sort optimization.

Optimization of a: If now both had a small portion of an array of fully ordered, the merge operation can skip

private static void __mergeSort(int[] array, int l, int r) {
		if(l>=r){
			return ;
		}
		
		int mid = (r-l)/2 + l;
		__mergeSort(array,l,mid);
		__mergeSort(array,mid+1,r);
		if(array[mid]>array[mid])//如果左侧最大的数小于右侧最小的数,说明有序,不需要再归并了
		__merge(array,l,r,mid);
	}

Optimization 2: When a small number of array elements, they can be optimized version of the insertion sort, sorting improve efficiency in the near-ordered array.

	private static void __mergeSort(int[] array, int l, int r) {
		if(r-l<=15){//如果个数小,其有序的可能性越大,尝试插入排序
			insertionSortBetter(array,l,r);
			return;
		}
		
		int mid = (r-l)/2 + l;
		__mergeSort(array,l,mid);
		__mergeSort(array,mid+1,r);
		if(array[mid]>array[mid])//如果左侧最大的数小于右侧最小的数,说明有序,不需要再归并了
		__merge(array,l,r,mid);
	}


    // 插入排序法优化
	public static void insertionSortBetter(int[] array,int l,int r) {
		for (int i = l+1; i <= r; i++) {
			int e = array[i];
			int j;
			for (j = i; j > l && e < array[j - 1]; j--) {
				array[j] = array[j - 1];
			}
			array[j] = e;
		}
 
	}

Optimization of three:

The top-down sort merge to merge sort bottom-up, reducing the operation of partitioning, the classical approach is not, not listed here ...

Guess you like

Origin blog.csdn.net/A1344714150/article/details/88699902