Merge sort adopts the thought of dividing and treating (divide) into some small problems and then solving them recursively, while the conquer stage will "mend" the answers that are obtained in the sub-stages, that is, divide and conquer. Merging refers to combining two or more ordered tables into a new, ordered table of two or more. Suppose that the table to be sorted with n elements, as an n ordered sub-table, each child table length is 1, and then 22 merge, get N/2 length 2 or 1 of the ordered table, continue to 22 merge, until merged into an ordered table of length n, this sort method is called 2 way merge sort.
Merging adjacent ordered sub-sequences
Combine [4,5,7,8] and [1,2,3,6] two ordered sub-sequences into the final sequence [1,2,3,4,5,6,7,8].
Reverse number: Merge sort divides the array a[l,h] into two halves a[l,mid] and a[mid+1,h] and then merges the two halves together. In the process of merging (set L<=i<=mid,mid+1<=j<=h), when A[i]<=a[j], no reverse number is generated; when A[i]>a[j], the number larger than A[i] in the first half is larger than a[j], will a[j] Put in front of a[i], reverse order number to add mid+1-i. Therefore, you can calculate the number of reverse orders in the merge process of the merging sort.
Source: Reverse order in the array
1 Public classMergeSort {2 //Array to sort3 Private int[] arr;4 //reverse to Total5 Private intresult;6 7 //Merge two arrays8 Private voidMergeintLeftintMidintRight ) {9 inti = left, J = mid + 1, k = 0;Ten int[] Temparr =New int[Right-left + 1]; One A while(I <= mid && J <=Right ) { - if(Arr[i] >Arr[j]) { -temparr[k++] = arr[j++]; theResult + = Mid + 1-i; - //Avoid overflow -Result%= 1000000007; -}Else { +temparr[k++] = arr[i++]; - } + } A at while(I <=mid) { -temparr[k++] = arr[i++]; - } - while(J <=Right ) { -temparr[k++] = arr[j++]; - } in -System.arraycopy (Temparr, 0, arr, left, K); to } + - //Merge Sort the Private voidMergeSort (intLeftintRight ) { * if(Left <Right ) { $ intMid = (left + right) >> 1;Panax Notoginseng MergeSort (left, mid); -MergeSort (Mid + 1, right); the merge (left, Mid, right); + } A } the + //Get reverse order number - Public intGetResult (int[] Array) { $ if(Array = =NULL|| Array.Length <= 1) { $ return0; - } - thearr =Array; -result = 0;WuyiMergeSort (0, Arr.length-1); the - returnresult; Wu } -}
Test Case:
1 Import Staticorg.junit.assert.*;2 3 ImportOrg.junit.Before;4 Importorg.junit.Test;5 6 Public classMergesorttest {7 mergesort mergesort;8 9 @BeforeTen Public voidSetUp ()throwsException { OneMergeSort =Newmergesort (); A } - - @Test the Public voidTest () { - int[] arr = {1, 2, 3, 4, 5, 6, 7, 0}; -Assertequals (7, Mergesort.getresult (arr)); - } + -}
Test results:
2-Way Merge sorting performance analysis:
Space efficiency: 2 sequential table merging requires a maximum of n cells of the auxiliary space, so the spatial complexity is O (n).
Time efficiency: Each trip merges the time complexity is O (n), need log2n to merge, so time complexity is O (nlog2n). The best, worst, and average time complexity are O (nlog2n).
Stability: 2 sequential table merging does not change the relative order of the same elements, so it is a stable sorting method.
The Arrays.sort () method in Java uses a sort algorithm called Timesort, which is an optimized version of the merge sort.
Resources
"2017 Data Structure review Guide" p304-305
The merging sort of graphical sorting algorithm (four)
Merge sort for reverse order
Data structure Merge sort