First, the basic idea
Merge sorts, dividing the current sequence into smaller sequential sequences, and then merging them into larger ordered sequences, one after the other. In this case, several small ordered sequences are divided into a sequence of n lengths of 1, and 22 are combined into ordered sequences of length two. The ordered sequence of length two is then combined into an ordered sequence of length four. And so on, the length of the original sequence is finally reached, so the sorting is done. This is actually a description of the recursive backtracking process of merge sort. The positive sequence description of the merge sort can be done by dividing the sequence into two sub-sequences, merging the two sub-sequences, and then merging the two subsequence into the original sequence.
Ii. Steps of implementation
Merge sort, there are two problems to be solved: first, how to divide the sub-interval. Second, how to merge the sub-range.
Dividing sub-ranges:
This is a two-way merge sort, which divides the current sequence into two sub-sequences.
Merge sub-ranges:
Because the sub-intervals are ordered, there is no significant complexity during the merge process, and a single traversal is possible.
Thought has been stated, the following look at the code, there are detailed comments.
Third, the implementation of code
Random array test class Click here
Packagesort;ImportSort.util.*; Public classMergingsortImplementsisort{//private static int[] c = null; //merge ordered sequence A[left] ~ A[middle] and a[middle+1] ~ A[right] into array B, return array B Private voidMergeint[] A,intLeft,intMiddle,intRight ) { int[] B =New int[A.length]; for(inti = 0; i < a.length; i++) {//Copy Array A to BB[i] =A[i]; } inti = left, j = middle + 1, k =Left ; while(I <= Middle && J <= right) {//merge process, select smaller, assign values one by one. if(B[i] < b[j]) a[k++] = b[i++]; Elsea[k++] = b[j++]; } //loop ends, indicating that an ordered sequence has been traversed while(I <= Middle) {a[k++] = b[i++];}//Copy the remaining parts directly into the a array while(J <= right) {a[k++] = b[j++];}//only one of these two loops can be executed . } //A[left] ~ a[right] to merge, sorted results placed in b[] Private voidMergeSort (int[] A,intLeft,intRight ) { if(Left! = right) {//When a merge sort array has only one element intMiddle = (left + right)/2;//divide into two parts to mergeMergeSort (A, left, middle);//merge sort on left partMergeSort (A, middle+1, right);//merge sort on right partMerge (A, left, middle, right);//Merge after the left and right sorts are complete } } Public voidSortint[] Array) {mergesort (array,0, Array.length-1); } Public Static voidMain (string[] args) {int[] array = randomarraygenerator.getrandomarray (100, 30); Sorttesthelper.test (NewMergingsort (), array); }}
Test results:
Iv. Summary and Analysis
Time complexity: O (n log n) combined complexity is n, the complexity of the recursive process is log2n
Space complexity: O (n) requires an auxiliary array
Stability: Stable.
The heap ordering and fast ordering of the remaining two Nlogn complexity in the eight-sort order are unstable, and the hill sort that is faster than the general sort is also unstable. In this paper, the merger sort is stable, and the efficiency is very considerable.
This article personal writing, the level is limited, if there are errors, please point out, welcome to discuss sharing
The sort of eight sorts of merging