Sort algorithm--merge sort (merge)

Source: Internet
Author: User

Merge sort is the use of recursive and divide-and-conquer technology to divide the data series into smaller Antimeron tables, and then sort the sub-table, and finally use recursive steps to merge the ordered Antimeron table into an increasingly large ordered sequence, the merge sort consists of two steps, respectively:

1) Divide sub-table 2) Merge Antimeron table

The time complexity is θ (NLGN), which is better than the insertion sorting algorithm.

Algorithm description
1) Apply space to make it the sum of two sorted sequences, which is used to store the combined sequence
2) set two pointers where the initial position is the starting position of the two sorted sequence
3) Compare the elements pointed to by two pointers, select a relatively small element into the merge space, and move the pointer to the next position
4) Repeat step 3 until a pointer reaches the end of the sequence
5) Copy all remaining elements of another sequence directly to the end of the merge sequence
Features: Merge sort is a stable sort. That is, the order of equal elements does not change, the speed is second only to the fast sort, but more stable. Algorithm implementation:
1 //Merge-sort2 voidMergeint*arry,Const intFirstConst intMidConst intLast )3 {4     intI,index;5     intFirst1,last1;6     intFirst2,last2;7     int*tmp;8Tmp= (int*)malloc((last-first+1)*sizeof(int));9     if(tmp==NULL)Ten         return ; Onefirst1=First ; Alast1=mid; -First2=mid+1; -Last2=Last ; theindex=0; -      while((First1<=last1) && (first2<=last2)) -     { -         if(arry[first1]<Arry[first2]) +         { -tmp[index++]=Arry[first1]; +first1++; A         } at         Else{ -tmp[index++]=Arry[first2]; -first2++; -         } -     } -      while(first1<=last1) in     { -tmp[index++]=arry[first1++]; to     } +      while(first2<=last2) -     { thetmp[index++]=arry[first2++]; *     } $      for(i=0; i<last-first+1; i++)Panax Notoginseng     { -arry[first+i]=Tmp[i]; the     } +      Free(TMP); A } the  + /*merge sort using recursion*/ - voidMerge_sort (int*arry,Const intFirstConst intLast ) $ { $     intMid=0; -     if(first<Last ) -     { theMid= (first+last)/2; - Merge_sort (arry,first,mid);WuyiMerge_sort (arry,mid+1, last); the merge (arry,first,mid,last); -     } Wu}

Additional: implementation is implemented using an iterative approach (personal sense recursion for code to look good)

1 /** * Using iterative implementations * **/2 voidMerge_sort (int*list,Const intFirstConst intLast )3 {4     intlen= last-first+1;5     intLeft_min,left_max;6     intRight_min,right_max;7     intindex;8     inti;9     int*tmp;TenTMP = (int*)malloc(sizeof(int)*len); One     if(tmp = = NULL | | Len <=0 ) A         return; -      -      for(i =1; i < Len; I *=2 ) the     { -          for(Left_min =0; Left_min < Len-i; Left_min =Right_max) -         { -             intJ; +Right_min = Left_max = Left_min +i; -Right_max = Left_max +i; +j =left_min; A             if(Right_max >len) atRight_max =Len; -index =0; -              while(Left_min < Left_max && Right_min <Right_max) -             { -tmp[index++] = (List[left_min] > list[right_min]? list[right_min++]: list[left_min++]); -             } in              while(Left_min <Left_max) -             { toList[--right_min] = list[--Left_max]; +             } -              while(Index >0 ) the             { *List[--right_min] = tmp[--index]; $             }Panax NotoginsengDisplay_array (j,i*2, list+j); -         } the     } +      Free(TMP); A}

Test Call Instance

1 intMain ()2 {3     intarry[]={1,2,5,6,4,7};4Merge_sort (Arry,0,5);5      for(intI=0;i<6; i++)6     {7cout<<arry[i]<<" ";8     }9System"Pause");Ten     return 0; One}

Blog reference: http://blog.chinaunix.net/uid-24467128-id-2606195.html

Http://www.cnblogs.com/jillzhang/archive/2007/09/16/894936.html

Sort algorithm--merge sort (merge)

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.