Review data structure: sorting algorithm (four)--merge sort

Source: Internet
Author: User

Basic idea: Based on the divide-and-conquer method, the sequence of arrays to be sorted is divided into several sub-sequences, each sub-sequence is sorted, and then all ordered sub-sequences are combined into a whole ordered sequence. The analysis shows that if you take any element as a sub-sequence, then all the subsequence is already ordered, and the key to merge sort is how to combine, that is, "merge".


The merge sort is an outer sort, a stable sort, and the time complexity is O (NLOGN).




In detail, the process of merging sorting: 1 Elements of the table are always ordered. So for a sequence of n elements, each element can be considered as 1 ordered sub-tables. The child table 22 is merged to generate the N/2 sub-table, except that the last child table may be 1 and the remaining child table lengths are 2. Then 22 merges until a table of n elements is ordered by key code.

The code is as follows:


Non-recursive implementation: (After debugging, the program has a bug, the subsequent correction!) )

#include <iostream>using namespace std; void Merge (int* R, int* rf, int i, int m, int n) {int J, K; for (j = m+1, k = i; I <= m && j <= N; k++)//I Record the first subsequence tag, J records the second subsequence tag, K records the combined sequence tag {if (R[j] < r[i])//deposit lesser rf[k] = r[j++]; Elserf[k] = r[i++];} while (I <= m)//If unequal, this situation occurs rf[k++] = r[i++]; while (j <= N) rf[k++] = r[j++]; for (int II = 0; II < n+1; ii++) cout<<rf[ii]<< "; cout<<endl; }void MergeSort (int* R, int* rf, int length) {int len = 1; int* q = r;//int* tmp; while (len < length) {int s = len; len = 2*s; Two sub-sequences of equal length, equal to int i = 0; BOOL flag = TRUE; while (i + Len <= length)//equal length of two sub-sequences merge {cout<< "equal length" <<i<< '-' <<i+s-1<< '-' <<i+ len-1<<endl;  Merge (q, RF, I, i+s-1, i+len-1);  Note the starting position of the marker i + = Len; The starting flag = False for the next merge; }if (i + S <= length)//Unequal length two subsequence merge {cout<< "unequal" <<i<< "-' <<i+s-1<< '-' << length-1 <<endl;  Merge (q, RF, I, i+s-1, length-1); Pay attention to the final endMark position}cout<< "flag=" <<flag<<endl;  if (!flag) swap (q, RF);  The Exchange pointer, the result of the merge, as the start of the next merge}}int main () {int a[10] = {11,1,5,7,2,4,9,6,10}; , 9,6,10,8int b[10]; MergeSort (A, B, 9); for (int i = 0; i< 9; i++) cout<<b[i]<< "; cout<<endl; return 0; }


Recursive implementations:

void Msort (Elemtype *r, elemtype *rf,int s, int t)  {       elemtype *rf2;      if (s==t) r[s] = Rf[s];      else      {           int m= (s+t)/2;          /* Split *p Table *          /Msort (R, Rf2, S, m);        /* Recursively merge P[S...M] into ordered p2[s...m]*/          Msort (R, Rf2, m+1, T);      /* Recursively merge p[m+1...t] into an ordered p2[m+1...t]*/          merge (Rf2, RF, S, m,t);   /* Merge P2[S...M] and p2[m+1...t] to p1[s...t]*/      }  }  void Mergesort_recursive (Elemtype *r, elemtype *rf, int n)  {   /* sort the Order table *p *      /Msort (R, rf,0, n-1);  }  


Reference:

http://blog.csdn.net/xiazdong/article/details/8462393

http://blog.csdn.net/hguisu/article/details/7776068

http://blog.csdn.net/jetjetlinuxsystem/article/details/6589093


Review data structure: sorting algorithm (four)--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.