Sorting algorithm Java implementation (merge sort)

Source: Internet
Author: User

1  Packagesorting;2 3 /**4 * Merge Sort5 * Average O (nlogn), preferably O (nlogn), Worst O (nlogn), Space complexity O (n), stable, more complex6  * @authorZeng7  *8  */9  Public classGuibingpaixu {Ten  One      Public Static voidMerge (int[] arr,intStartIndex,intMidindex, A             intEndIndex) { -         int[] Temparr =New int[arr.length]; -System.out.println ("Merge" + StartIndex + "~" +endIndex); the         inti = startIndex, j = midindex + 1, k =StartIndex; -          while(I! = Midindex + 1 && J! = EndIndex + 1) { -             if(Arr[i] >Arr[j]) -temparr[k++] = arr[i++]; +             Else -temparr[k++] = arr[j++]; +         } A          while(I! = Midindex + 1) attemparr[k++] = arr[i++]; -          while(J! = EndIndex + 1) -temparr[k++] = arr[j++]; -          for(i = startIndex; I <= endIndex; i++) -Arr[i] =Temparr[i]; -          for(intP:arr) inSystem.out.print (P + ""); - System.out.println (); to     } +  -     Static voidMergeSort (int[] arr,intStartIndex,intEndIndex) { the         intMidindex; *         if(StartIndex <EndIndex) { $Midindex = (StartIndex + endIndex)/2;Panax NotoginsengMergeSort (arr, StartIndex, Midindex);//Left Order -MergeSort (arr, Midindex + 1, endIndex);//Right Order the Merge (arr, StartIndex, Midindex, endIndex); +         } A     } the  +      Public Static voidMain (string[] args) { -         int[] B = {49, 38, 65, 97, 76, 13, 27, 50 }; $MergeSort (b, 0, b.length-1); $          for(inti:b) -System.out.print (i + "")); -  the     } -}

Sorting algorithm Java implementation (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.