Sorting algorithm (4)--merge sort

Introduction: Merge sort is an effective sorting algorithm based on the merge operation, which is a very typical application of the divide and conquer method (Divide and Conquer) .

Merge the ordered subsequences to obtain a completely ordered sequence; that is, first make each subsequence ordered, and then make the subsequence segments ordered. If two sorted lists are merged into one sorted list, it is called two- way merge .

 

1. Main steps

The array to be sorted [0...n-1] is regarded as n ordered sequences of length 1, and adjacent ordered lists are merged in pairs to obtain n/2 ordered lists of length 2; Merge these ordered sequences again to obtain n/4 ordered sequences of length 4; and so on, and finally obtain an ordered sequence of length n.

To sum up:

Merge sort actually does two things:

(1) "Decomposition" - Divide the sequence in half each time .

(2) "Merge" - merge the divided sequence segments in pairs and then sort them .

 

2. Demonstration process
1. First look at the merger
(1) The ordered sub-arrays adjacent to both ends, arr[start]~arr[mid] is called array A and arr[mid+1]~arr[end] is called array B
(2), each time a value is taken from array A and array B and compared, the smaller one is placed in temporary array C
(3) When the data in array A and array B are finally fetched, array C is an ordered merged array.
(4), and finally copy the temporary array to the original array
2. Looking at the decomposition
(1) First divide the array into the smallest, gap=1, and then merge and sort the two adjacent data
(2), then set gap=2, and continue to merge the two adjacent arrays. If the number of sub-tables is odd, the last sub-table does not need to be merged with other sub-tables (that is, this round of processing is bye);
            If the number of sub-tables is even, it should be noted that the upper limit of the interval of the latter sub-table in the last pair of sub-tables is n-1.
(3), until the gap is equal to the length of the array, the array is merged
 
as the picture shows:


 
 
 
3. Code Implementation
@Override
public void sort(int[] arr) {
	mergeSort(arr);
}

private void merge(int[] array,int start,int mid,int end){
	int temp[] = new int[end-start+1];//Temporary array
	
	int firstArrIndex = start;//The subscript of the first array sequence
	int secondArrIndex = mid+1;//Subscript of the second array sequence
	int tempArrIndex = 0;//Temporarily store the index of the array
	
	//1. Scan the first array sequence and the second array sequence
	while(firstArrIndex <=mid && secondArrIndex<=end){
		//1.1 When the first array is smaller than the unsorted first element of the second array
		if(array[firstArrIndex] <=array[secondArrIndex]){
			temp[tempArrIndex] = array[firstArrIndex];
			firstArrIndex++;
		}else{//1.2 When the second array is smaller than the unsorted first element of the first array
			temp[tempArrIndex] = array[secondArrIndex];
			secondArrIndex++;
		}
		
		tempArrIndex++;
	}
	
	//2. When the first segment is not copied completely, copy all the remaining arrays to the temporary array
	while(firstArrIndex<=mid){
		temp[tempArrIndex] = array[firstArrIndex];
		firstArrIndex++;
		tempArrIndex++;
	}
	
	
	//3. When the second segment is not copied completely, copy all the remaining arrays to the temporary array
	while(secondArrIndex<=end){
		temp[tempArrIndex] = array[secondArrIndex];
		secondArrIndex++;
		tempArrIndex++;
	}
	
	//4. Copy the temporary array to the original array
	for(tempArrIndex=0,firstArrIndex=start;firstArrIndex<=end;tempArrIndex++,firstArrIndex++){
		array[firstArrIndex] = temp[tempArrIndex];
	}
}

private void mergeSort(int[] arr){
	for (int gap = 1; gap < arr.length; gap = 2 * gap) {
		int i=0;
		//Merge two adjacent subarrays of gap length
		for(i=0; i+2*gap-1< arr.length; i = i + 2*gap) {
			merge(arr, i, i+gap-1, i+2*gap-1);
		}
		
		// Less than two merged subarrays remain. Make sure that the first array gap exists.
		if(i + gap - 1 < arr.length){
			merge(arr, i, i + gap - 1, arr.length - 1);
		}
	}
}
 

 

Guess you like

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