1. Algorithm idea
- Split the data collection two minutes apart
- Loop split to only one left of each group
- Sort a split array into combinations
- 22 merge until you merge into an array, sorting is complete
- Algorithmic Thinking Reference
2. code example
Packagecom.tianxiapai.admin.interfaces.commons; Public classDemo { Public Static int[] array = {23, 11, 7, 29, 33, 59, 8, 20, 9, 3, 2, 6, 10, 44, 83, 28, 5, 1, 0, 36}; /*** Merge sort: Split the data set two minutes until the smallest 22 sort merge*/ Private Static voidmergesort () {int[] temp =New int[Array.Length]; MergeSort (temp,0, Array.length-1); } /*** Check the score group, if the array cannot be split, then return directly, split after merging*/ Private Static voidMergeSort (int[] temp,intStartintend) { if(Start >=end) { return; } intMid = (start + end)/2; MergeSort (temp, start, mid); MergeSort (temp, Mid+ 1, end); Mergearray (temp, start, mid+ 1, end); } /*** Combine array of arrays, mid-centered, front and back two arrays*/ Private Static voidMergearray (int[] temp,intStartintMidintend) { //define pointer subscript, record before and after paragraph is enough to continue moving intMinA = start, Minb =mid; for(inti = start; I <= end; i++) { if(MinA >= Mid | | minb >end) { //If A or B is exhausted, use the if(MinA >=mid) {Temp[i]=ARRAY[MINB]; MINB++; } Else{Temp[i]=Array[mina]; MinA++; } } Else { //They're all useless . if(Array[mina] <ARRAY[MINB]) {Temp[i]=Array[mina]; MinA++; } Else{Temp[i]=ARRAY[MINB]; MINB++; }}} system.arraycopy (temp, start, array, start, end-Start + 1); for(inti=0;i<array.length;i++) {System.out.print (Array[i]+" "); } System.out.println (""); } Public Static voidMain (string[] args) { for(inti=0;i<array.length;i++) {System.out.print (Array[i]+" "); } System.out.println (""); MergeSort (); System.out.println (""); for(inti=0;i<array.length;i++) {System.out.print (Array[i]+" "); } } }
Operation Result:
3. Complexity of Time
- Time complexity O (NLOGN)
Common algorithm Value Merge sort