Merge sort Java implementation and its actual efficiency comparison

The Java implementation of merge sort, compared with the efficiency of Hill sort

Merge sort is a typical application of divide and conquer

The main idea is to continuously divide an array into two (two-way merging), until it can no longer be divided, adjust the position between the two numbers, and then combine the adjusted sequences of each part to get a whole. ordered array.

The specific process refers to the encyclopedia merge sort

A Java implementation of merge sort:

package com.ryo.algorithm.sort;

import com.ryo.algorithm.sort.Sort;

/**
 * merge sort
 * @author shiin
 */
public class MergeSort implements Sort{
	
	private int[] result;
	private int temp;

	@Override
	public int[] sort(int[] arr) {
		result = new int[arr.length];
		sort(arr , 0 ,arr.length-1);
		return result;
	}
	
	public void sort(int[] arr ,int begin ,int end) {
		if(end - begin <= 1) {
			if(arr[begin] > arr[end]) {
				temp = arr[begin];
				arr[begin] = arr[end];
				arr[end] = temp;
			}
		}
		else {
			int mid = (begin + end)/2;//The middle value is taken here. I used Math.floor at first, which consumes an average of 3-4ms in the test of 100,000 random integers of 0-100000
			sort(arr ,begin ,mid);
			sort(arr ,mid+1 ,end);
			mergeSort(arr ,begin ,mid ,end);
		}
	}
	
	public void mergeSort(int[] arr ,int begin ,int mid ,int end) {
		int i = begin;
		int j = mid+1;
		int index = begin;
		// Compare the two arrays to be merged
		// move the smaller number into the temporary storage array
		while(i <= mid && j <= end) {
			if(arr[i] > arr[j]) {
				result[index++] = arr[j++];
			}
			else {
				result[index++] = arr[i++];
			}
		}
		// move the remaining part into result
		if(j > end) {
			for(;i < mid+1;i++) {
				result[index++] = arr[i];
			}
		}
		else {
			for(;j < end+1;j++) {
				result[index++] = arr[j];
			}
		}
		//copy the array back to the original array
		for(i=begin ;i<end+1 ;i++) {
			arr[i] = result[i];
		}
	}
}

Practical efficiency of merge sort

Test environment: Jdk1.8.0_161 eclipse Version: Oxygen.3a Release (4.7.3a)

Sort 100000 random integers from 0-100000 result:

Test results using Hill sorting in the same environment:

The difference between the two algorithms is only about 3ms in the case of 100,000 data, but even if the Java implementation of Hill sort or merge sort is the same, depending on its code, there may be a gap in efficiency, and this gap may be completely 3ms level.

And the efficiency of Hill sorting has a lot to do with its step size, where the step size is: private int[] steplist ={1607,733,373,181,83,37,17,7,3,1};

Since there is no obvious difference between the two algorithms under the test of 100,000 data, it is expanded to 1 million for testing, and the stable results after multiple runs are taken:

Merge sort result:

Hill sort result:

With a data volume of one million, due to the increase of the data volume, the impact of errors caused by specific code details is relatively reduced. It can be seen that the efficiency of merge sorting is more than twice that of Hill sorting.

 

In addition , the efficiency comparison between Hill sort and insertion sort

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325659054&siteId=291194637