basic idea
Merge sort (MERGE-SORT) is a sorting method implemented by the idea of merging . The algorithm adopts the classic divide -and-conquer strategy ( divide-and-conquer method divides the problem into small problems and then solves them recursively. , and the conquer stage "fixes" the answers obtained in the division stage together, that is, divide and conquer ).
divide and conquer
It can be seen that this structure is very similar to a complete binary tree. The merge sort in this article is implemented recursively (or iteratively). Staged can be understood as the process of recursively splitting subsequences, and the recursion depth is log 2 n.
Merge adjacent ordered subsequences
Let's look at the governance stage again, we need to merge two already ordered subsequences into an ordered sequence, such as the last merge in the above figure, to [4,5,7,8] and [1,2, 3,6] Two already ordered subsequences are merged into the final sequence [1,2,3,4,5,6,7,8], let's see the implementation steps.
Code
package sortdemo; import java.util.Arrays; /** * Created by chengxiao on 2016/12/8. */ public class MergeSort { public static void main(String []args){ int []arr = {9,8,7,6,5,4,3,2,1}; sort(arr); System.out.println(Arrays.toString(arr)); } public static void sort( int []arr){ int []temp = new int [arr.length]; // Before sorting, build a temporary array with a length equal to the length of the original array to avoid frequent opening of space in recursion sort (arr,0,arr.length-1 ,temp); } private static void sort(int[] arr,int left,int right,int []temp){ if(left<right){ int mid = (left+right)/2; sort(arr,left,mid,temp); // Merge sort on the left, so that the left subsequence is ordered sort(arr,mid+1,right,temp); // Merge and sort on the right, so that the right subsequence is ordered merge( arr,left,mid,right,temp); // Merge two ordered subarrays } } private static void merge( int [] arr, int left, int mid, int right, int [] temp){ int i = left; // left sequence pointer int j = mid+1; // right sequence pointer int t = 0; // Temporary array pointer while (i<=mid && j<= right){ if (arr[i]<= arr[j]){ temp[t++] = arr[i++]; }else { temp[t++] = arr[j++]; } } while (i<=mid){ // fill the remaining elements on the left into temp temp[t++] = arr[i++ ]; } while (j<=right){ // fill the remaining elements of the right sequence into temp temp[t++] = arr[j++ ]; } t = 0 ; // Copy all the elements in temp to the original array while (left <= right){ arr[left++] = temp[t++]; } } }
Results of the
[1, 2, 3, 4, 5, 6, 7, 8, 9]
finally
Merge sort is a stable sorting, and it is also a very efficient sorting. The sorting performance that can take advantage of the characteristics of a complete binary tree is generally not too bad. Arrays.sort() in java uses a sorting algorithm called TimSort, which is an optimized version of merge sort. As can be seen from the figure above, the average time complexity of each merge operation is O(n), while the depth of a full binary tree is |log2n|. The overall average time complexity is O(nlogn). Moreover, the best and worst merge sort has an average time complexity of O(nlogn).