Sorting algorithm of data structure and algorithm (IV.): merge sort

Source: Internet
Author: User

Merge sort (application of divide and conquer thought)

principle : The two (or more than two) ordered tables are combined into a new ordered table, that is, the ordered sequence is divided into several sub-sequences, each sub-sequence is ordered, and then the ordered subsequence is combined into a whole day ordered sequence.

Divide and conquer the algorithm step:

First step: Divide. Divide the original problem into several sub-problems

The second step: recursive solution. Solving each sub-problem recursively

Step three: Merging. Merging the solved sub-problems into the original problem

Code implementation (Recursive):
voidMergeSort (int[] arr) {sort (arr,0,arr.length-1);}voidSortint[] arr,intLeftintRight ) {    if(left >= right) {return;} //Find the intermediate index    intMiddle = left + (right-left)/2; //sort the left arraysort (arr,left,middle); ////sort the right arraySort (Arr,middle + 1, right); //Mergingmerge (Arr,left,middle,right); Print (arr);}/*Merges two arrays, merges the first two arrays in order, and then merges the ordered parameter list: arr: Array object. Left: The index of the first element of the leftmost array. Middle: The last element index of the left array. Right: The last element index of the left array*/voidMergeint[] arr,intLeftintMiddle,intRight ) {     //Temporary Array    int[] Tmparr =New int[Arr.length]; //index of the temporary array    intTmparrindex =Left ; //the first index of the right array    intRightfirst = middle + 1; //the index of the first element of the cache array    intTMP =Left ;  while(left <= Middle && rightfirst <=Right ) {        //Remove the smallest set of temporary arrays from two sub-arrays        if(Arr[left] <=Arr[rightfirst]) {Tmparr[tmparrindex+ +] = arr[left++]; }Else{Tmparr[tmparrindex+ +] = arr[rightfirst++]; }    }    //The remainder is placed in a temporary array     while(Left <=middle) {Tmparr[tmparrindex+ +] = arr[left++]; }     while(Rightfirst <=Right ) {Tmparr[tmparrindex+ +] = arr[rightfirst++]; }    //copies the contents of the temporary array back into the original array     while(TMP <=Right ) {Arr[tmp]= tmparr[tmp++]; }}voidPrintint[] arr) {     for(intI:arr) {System.out.print (i+ "\ T"); }}

Analysis : algorithm stability, spatial complexity "array O (N+LOGN) Logn for recursion when using the depth of Logn stack space, linked list O (Logn)", Time complexity "best, average, Worst is O (Nlogn)"

Code implementation (non-recursive):

voidMergesortnonrecursive (int[] arr) {    if(arr = =NULL|| Arr.length <= 1) {return;} //The current merged sub-sequence has a length of 1    intLen = 1;  while(Len <=arr.length) {    //Each time from the first element of the array, the adjacent subsequence is merged with each len*2 element, and the processing of the merge length is odd and the last merge segment and the previous one are not equal to each other.         for(inti=0; I+len <= arr.length-1; i + = Len * 2){           //index of the first element of the left array            intleft =i; //the last element index of the left array            intMID = i + len-1; //the last element index of the right array            intright = i + len * 2-1; //Boundary Processing            if(Right > Arr.length-1) {right = Arr.length-1;} //Merge reference recursive sort of mergemerge (Arr,left,mid,right); }Len *= 2; }    return;}

Analysis : In the space to avoid recursion when the depth of the "O" (logn) of the stack space, in time, to avoid recursion, in time also improved.

Sorting algorithm of data structure and algorithm (IV.): 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.