Data Structures (3) Merge sort

Source: Internet
Author: User

Merge Sort
    • Merge sort principle

Merge sort (merging sort) is a sort method that is implemented by merging ideas. Its principle is that the initial sequence contains n records, it can be regarded as n ordered sub-sequence, each subsequence length of 1, and then 22 merge, to get [N/2] length of 2 or 1 ordered sub-sequence, and then 22 merge, until a length of an ordered sequence of N, this sort method becomes 2 way merge sort.

    • Look at the code first
Package Sort;class mergesort {public static void main (string[] args) {int[] list = {80,40,50,30,60,70,10,90,20};; MergeSort ms = new MergeSort (); Ms. Msort (list); System.out.println (""); SYSTEM.OUT.PRINTLN ("final result:"); Ms.printarrry (list);} private void Printarrry (int[] a) {for (int k = 0; k <= a.length-1; k++) {System.out.print (a[k]);}} private void Msort (int[] list) {mergesort (list, 0, list.length-1);} private void MergeSort (int [] List,int A, int b) {int mid;if (a = = b) {//system.out.println ("execute to the bottom:");//printarrry (list); r Eturn; }else {mid = (a+b)/2;mergesort (list, A, mid), MergeSort (list, mid+1, b); Merge (list, A, Mid, b); System.out.println ("\ n" + "Left:" +a+ "Mid:" +mid+ "right:" +b "); System.out.println ("At this time the array is:");p Rintarrry (list);}} private void Merge (int [] list, int left, int mid, Int. right) {int a,b,c,d;a = Left;b = Mid+1;c = Left;d = left;int[] Tempa DDR = new Int[list.length];while (a<=mid && b<=right) {if (List[a] <= list[b]) {tempaddr[c++] = list[a++] ;} else {tempaddr[c++] = List[b++];}} while (A<=mid) {tempaddr[c++] = list[a++];} while (B<=right) {tempaddr[c++] = list[b++];} while (d <= right) {list[d] = tempaddr[d++];}}}
    • MergeSort () analysis
private void MergeSort (int [] List,int A, int b) {int mid;/* when split to the smallest unit, jumps out of */if (a = = b) {return;} else {mid = (a+b)/2;/* splits the first half of the list of the last split array */mergesort (list, A, mid), and/* Splits the second half of the array list after the last split */mergesort (list, Mid+1, b);/* Splits the first and second halves of the list's smallest cell into a merge */merge (list, A, Mid, b); System.out.println ("\ n" + "Left:" +a+ "Mid:" +mid+ "right:" +b "); System.out.println ("At this time the array is:");p Rintarrry (list);}}
    • Merge () analysis
private void Merge (int [] list, int left, int mid, Int. right) {int a,b,c,d;a = Left;b = Mid+1;c = Left;d = left;int[] Tempa DDR = new int[list.length];/* compares two ordered arrays, starting from scratch, with smaller values deposited tempaddr[]*/while (A<=mid && b<=right) {if (List[a] <= List[b]) {tempaddr[c++] = list[a++];} else {tempaddr[c++] = list[b++];}} /* Remove the first half of the remaining array, and deposit Tempaddr[]*/while (A<=mid) {tempaddr[c++] = list[a++];} /* Remove the remaining array from the second half and deposit Tempaddr[]*/while (b<=right) {tempaddr[c++] = list[b++];} /* The array that has been merged is passed to list so that the list original corresponds to List[left. Right] part ordered */while (d <= right) {list[d] = tempaddr[d++];}}
    • Output results
left:0 mid:0 right:1 The array is: 408050306070109020left:0 mid:1 right:2 at this time the array is: 405080306070109020left:3 Mid:3 right : 4 The array is: 405080306070109020left:0 mid:2 right:4 at this time the array is: 304050608070109020left:5 mid:5 right : 6 The array is: 304050608010709020left:7 mid:7 right:8 at this time the array is: 304050608010702090left:5 mid:6 right : 8 At this point the array is: 304050608010207090left:0 mid:4 right:8 The array is: 102030405060708090 final result: 102030405060708090
    • Schematic diagram

The red section shows the interval of the array of operations at this time, and the black part, which is the interval that is not being manipulated.

As you can see from this graph, because mid= (a+b)/2,mergesort (list, A, mid), the list array is split all the way down, from 0-8, to 0-4, to 0-2, to 0-1, and finally to 0-0, at which point return returns, returning to the upper level of recursion, At this point the mid is 0,mergesort (list, mid+1, b), and you can get the return, then execute the merge (list, A, Mid, b). This sentence is to merge at this time (a,mid,b) = (0,0,1), so that list[0..1] in order.

And so on, followed by (a,mid,b) = (0,1,2), (a,mid,b) = (0,2,4), ...., (a,mid,b) = (5,6,8), (a,mid,b) = (0,4,8)

    • Analysis of time complexity

LIST[1..N] of the adjacent length of an ordered sequence of H 22 merge, and put the results back to LIST[1..N], it takes time O (n), by the depth of the full binary tree to restrain, the entire sort needs to be log2n times, so the total time complexity is O (NLOGN). This is faster than the bubble sort of the previous story, and the direct selection sort is a much wider application.

Data Structures (3) 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.