Java sort Merge sort 1. Brief introduction
The algorithm of merging sorting is to combine multiple ordered data tables into an ordered data table. If there are only two ordered tables participating in the merge, it becomes a two-way merge. For a primitive column to be sorted, it can often be attributed to a multi-path merge sort by means of segmentation.
2. Merging and sorting ideas
- Consider a sorted array of length n to consist of an array of n ordered lengths of 1
- Merge its 22 to get an ordered array of length 2
- Then merge these sub-tables to get an ordered array of length 4
- Repeat the process until the last sub-table length is n and the sort is done
3. Code examples
There are two implementations of merge sort: recursive and non-recursive. Take a look at how and merge two ordered arrays before looking at merge sorted code:
//Base, merge two ordered arrays Public Static int[]Merge2arr(int[] arr1,int[] arr2) {intLen1 = arr1.length;intLen2 = arr2.length;int[] res =New int[Len1 + len2];//Use an array to store the sorted array inti =0, j =0, k =0; while(I < len1 && J < Len2) {res[k++] = Arr1[i] < Arr2[j]? arr1[i++]: arr2[j++]; } while(I < LEN1) {res[k++] = arr1[i++]; } while(J < Len2) {res[k++] = arr2[j++]; }returnRes;}
Recursive implementation of merge sort:
//merge sort, recursive implementation Public void sortmergerecursion(int[] nums) {Sortmergerecursionhelper(Nums,0, Nums.length-1);} Public void Sortmergerecursionhelper(int[] Nums,intLeftintright) {if(left = right)return;//When the sequence length to be sorted is 1 o'clock, the recursion begins backtracking, and the merge intMiddle = left + (right-left)/2;Sortmergerecursionhelper(Nums, left, middle);Sortmergerecursionhelper(Nums, Middle +1, right);Mergearr(Nums, left, middle, right);} Public void Mergearr(int[] Nums,intLeftintMiddle,intright) {int[] tem =New int[Right-left +1];inti = left, J = Middle +1, k =0; while(I <= Middle && J <= right) {tem[k++] = Nums[i] < Nums[j]? nums[i++]: nums[j++]; } while(I <= Middle) {tem[k++] = nums[i++]; } while(J <= right) {tem[k++] = nums[j++]; }//writes auxiliary array data to the original array intindex =0; while(left <= right) {nums[left++] = tem[index++]; }}
Non-recursive implementations of merge sorts (iterations):
//merge sort, non-recursive implementation (iteration) Public void sortmergeiteration(int[] nums) {intLen =1;//The length of the initial sorted array while(Len < Nums.length) { for(inti =0; I < Nums.length; i + = Len *2) {Sortmergeiterationhelper(Nums, I, Len); } Len *=2;//The length of the array to be sorted each time}}/*** Auxiliary Functions * @param numsOriginal Array * @param startstarting from the start position * @param lenarray length for this merge */ Public void Sortmergeiterationhelper(int[] Nums,intStartintLen) {int[] tem =New int[Len *2];inti = start;intj = start + len;intK =0; while(I < start + len && (J < start + Len + len && J < Nums.length) {tem[k++] = Nums[i] < Nums[j]? nums[i++]: nums[j++]; } while(I < start + len && i < Nums.length) {//Note: I may also exceed the length heretem[k++] = nums[i++]; } while(J < start + Len + len && J < Nums.length) {tem[k++] = nums[j++]; }intright = start + len + len;intindex =0; while(Start < Nums.length&& Start < right) {nums[start++] = tem[index++]; }}
The time complexity of merge sort is O (n*log2n), the space complexity is O (n)
Merge sort is a stable sort method.
Reference:
50942604
Merge sort of Java sort