Basic sorting: Merge (merge) sorting is the combination of two (or more than two) ordered tables into a new ordered table, that is, to sort the sequence into a number of sub-sequences, each sub-sequence is ordered. Then the ordered subsequence is combined into a whole ordered sequence.
1 Public Static voidMain (string[] args) {2 intA[] = {34, 8, 64, 51, 32, 21};3MergeSort (A, 0, a.length-1);4 for(inti = 0; i < a.length; i++) {5System.out.print (A[i] + "");6 }7 }8 9 Public Static voidMergeSort (int[] A,intLeftintRight ) {Ten if(Left <Right ) { One intCenter = (left+right)/2; A MergeSort (A, left, center); -MergeSort (A, center+1, right); - merge (A, left, center, right); the } - } - - Public Static voidMergeint[] A,intLeftintCenterintRight ) { + int[] Tmparr =New int[a.length]; - intMID = Center+1; + intThird =Left ; A intTMP =Left ; at - while(Left <= Center && mid <=Right ) { - if(A[left] <=A[mid]) { -tmparr[third++] = a[left++]; -}Else { -tmparr[third++] = a[mid++]; in } - } to while(Mid <=Right ) { +tmparr[third++] = a[mid++]; - } the while(Left <=Center) { *tmparr[third++] = a[left++]; $ }Panax Notoginseng - while(TMP <=Right ) { theA[TMP] = tmparr[tmp++]; + } A}
Java Merge sort