Java Merge sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.