1 #include<iostream> 2 using namespace std; 3 int n; 4 void print(int *a) 5 { 6 for(int i=0;i<n;i++) 7 { 8 cout<<a[i]<<" "; 9 } 10 cout<<endl; 11 } 12 void merge(int *a,int *t,int begin,int mid,int end) 13 { 14 int xb=begin; 15 int b=begin; 16 int e=mid+1; 17 while(b<=mid&&e<=end) 18 { 19 if(a[b]<=a[e]) 20 { 21 t[xb++]=a[b++]; 22 } 23 else 24 { 25 t[xb++]=a[e++]; 26 } 27 }/*The total finished recording first side, left side as well, so even continue entering * / 28 the while (B <= MID) 29 { 30 T [XB ++] = A [B ++ ]; 31 is } 32 the while (E <= End ) 33 is { 34 is T [XB ++] = A [E ++ ]; 35 } 36 / * final step * / 37 [ for ( int I = the begin; I <= End; I ++ ) 38 is { 39 A [I] = [I] T ; 40 } 41 is } 42 is void msort ( int* A, int * T, int the begin, int End) 43 is { 44 is IF (the begin> = End) 45 { 46 is return ; / * do not meet the specifications can not be sorted * / 47 } 48 the else 49 { 50 int MID = (the begin + End) / 2 ; / * with mid to be sorted array is divided into two parts * / 51 is msort (A, T, the begin, mid); 52 is msort (A, T, mid + . 1 , End); 53 is Merge (A, T, the begin, MID, End); / * post-merge sorted to* / 54 is } 55 } 56 is void mergesort ( int * A) / * Package at * / 57 is { 58 int * T; 59 T = new new int [n-]; 60 msort (A, T, 0 , N- . 1 ); 61 is Free (T); 62 is } 63 is int main () 64 { 65 COUT << " next enter the problem size n-\ n- " ; 66 CIN >> n-; 67 int *A; 68 A = new new int [n]; 69 COUT << " next enter digits n \ n " ; 70 for ( int I = 0 ; I <n; I ++ ) 71 is { 72 CIN >> A [I] ; 73 is } 74 mergesort (a); 75 COUT << " below are the results sorted \ n- " ; 76 Print (a); 77 } 78 / * 79 16 80 . 11 33 is 0. 3 -4 -5 222 45 111 - 2. 8. 6. 7 516 789 84 81 * /
Recursive writing is easy Well, there are enough ideas to write, first to meet an array of sequence length is divided into two parts, the two parts can then have a recursive call, the two parts are calling the end, like the last merger, the combined use of merge function, you can combine a closer look at the code and comments.
1 #include<iostream> 2 using namespace std; 3 int n; 4 void print(int *a) 5 { 6 for(int i=0;i<n;i++) 7 { 8 cout<<a[i]<<" "; 9 } 10 cout<<endl; 11 } 12 void merge(int *a,int *t,int begin,int mid,int end) 13 { 14 int xb=begin; 15 int b=begin; 16 int e=mid+1; 17 while(b<=mid&&e<=end) 18 { 19 if(a[b]<=a[e]) 20 { 21 t[xb++]=a[b++]; 22 } 23 else 24 { 25 t[xb++]=a[e++]; 26 } 27 }/*The total finished recording first side, left side as well, so even continue entering * / 28 the while (B <= MID) 29 { 30 T [XB ++] = A [B ++ ]; 31 is } 32 the while (E <= End ) 33 is { 34 is T [XB ++] = A [E ++ ]; 35 } 36 / * final step * / 37 [ for ( int I = the begin; I <= End; I ++ ) 38 is { 39 A [I] = [I] T ; 40 } 41 is } 42 is void msort ( int* A, int * T) 43 is { 44 is int S = . 1 ; 45 int Times = . 1 ; 46 is int I; 47 the while (S < n-) 48 { 49 I = 0 ; 50 for (I = 0 ; I + 2 * S <n-; I = I + 2 * s) / * two cycles, each of the ordered sequence of length 2 * s, and then merge with the original input array * / 51 is { 52 is int MID = (I + I + 2 * S- 1 ) / 2; 53 merge(a,t,i,mid,i+2*s-1); 54 } 55 int mid=(i+n-1)/2; 56 merge(a,t,i,mid,n-1); 57 s*=2; 58 times++; 59 } 60 merge(a,t,0,s/2-1,n-1); 61 } 62 void mergeSort(int *a) 63 { 64 int * T; 65 T = new new int [n-]; 66 msort (A, T); 67 Free (T); 68 } 69 int main () 70 { 71 is COUT << " next enter the problem size n \ n " ; 72 CIN >> n; 73 is int * A; 74 A = new new int [n]; 75 COUT << " next enter digits n \ n " ; 76 for ( int I = 0; I <n-; I ++ ) 77 { 78 CIN >> A [I]; 79 } 80 mergesort (A); 81 COUT << " Below are the results sorted \ n- " ; 82 Print (A); 83 } 84 / * 85 . 19 86 . 11 330-5-4345222 111-7895162687 -28 -45 -456 84 87 * /
Here the wording is iterative, because it is iterative, so with two cycles, the first cycle which two adjacent ordered, then the adjacent four ordered. . . Until the maximum power is less than or equal to n 2. Taking into account necessarily exactly n is a power of 2, the layer 2 loop ends, and finally merge again.