Two-way merge sort
Merge sort adopted a " Divide and Conquer " strategy: The original problem was decomposed into n small size and the structure and the original problem similar sub-problem, the recursive solution of these sub-problems, and then merge the results, so as to get the original problem solution.
The divide-and-conquer model generally follows the following three steps:
decomposition (Divide): Decomposition of the original problem into a number of sub-problems;
Solve (Conquer): Solve each sub-problem recursively. If the sub-problem is small enough, it is solved directly;
merge (Combine): The solution of the sub-problem is merged into the solution of the original problem.
The "Two-way merge" algorithm also follows the following three steps:
decomposition: the N elements owned in the original sequence are decomposed into sub-sequences containing N/2 elements;
Solution: Recursive ordering of two sub-sequences by the combined sorting method;
Merging: merging two sorted sub-sequences to get the solution of the original sequence.
#define INF Numeric_limits<int>::max ()
Two-way merge sort for Sentinel mode:
voidMerge (intArr[],intStartintMidintrear) { inti,j; intm = Mid-start +1;//including [Start, Mid] intn = rear-mid;//including (Mid, rear] int* Leftarr =New int[M +1]; int* Rightarr =New int[n +1]; for(i =0; I < m; i++) Leftarr[i]= Arr[start +i]; for(j =0; J < N; J + +) Rightarr[j]= Arr[mid + j +1]; LEFTARR[M]= INF;//with a sentry.Rightarr[n] =INF; I= j =0; for(intK = start; K <= Rear; k++) { if(Leftarr[i] <Rightarr[j]) {Arr[k]= leftarr[i++]; } Else{Arr[k]= rightarr[j++]; }} cout<<"Merging ==>"; Show (arr,6); Delete[] Leftarr; Delete[] rightarr;}voidMergesorting (intArr[],intStartintrear) { if(Start <rear) { intMid = (start + rear)/2; cout<<"Middle is:"<< Mid <<Endl; Mergesorting (arr, start, mid); Mergesorting (arr, Mid+1, rear); Merge (arr, start, mid, rear); Show (arr,6); } Else { return; }}View Code
Two-way merge sort without Sentinels:
voidMerge2 (intArr[],intStartintMidintrear) { intI,j, K; intm = Mid-start +1;//including [Start, Mid] intn = rear-mid;//including (Mid, rear] int* Leftarr =New int[M]; int* Rightarr =New int[n]; for(i =0; I < m; i++) Leftarr[i]= Arr[start +i]; for(j =0; J < N; J + +) Rightarr[j]= Arr[mid + j +1]; I= j =0; for(k = start; I < m && J < N; k++) { if(Leftarr[i] <Rightarr[j]) {Arr[k]= leftarr[i++]; } Else{Arr[k]= rightarr[j++]; } } if(I <m) for(intx =0; x < m-i; X + +) Arr[k+ +] = leftarr[i +x]; if(J <N) for(inty =0; Y < n-j; y++) Arr[k+ +] = rightarr[j +y]; cout<<"Merging ==>"; Show (arr,6); Delete[] Leftarr; Delete[] rightarr;}voidMergeSorting2 (intArr[],intStartintrear) { if(Start <rear) { intMid = (start + rear)/2; cout<<"Middle is:"<< Mid <<Endl; MergeSorting2 (arr, start, mid); MergeSorting2 (arr, Mid+1, rear); Merge2 (arr, start, mid, rear); Show (arr,6); } Else { return; }}View Code
Test code:
intMain () {intA[] = {1,4,2,7,Ten,5 }; //Insertion_sort_asc (A, 6); //Insertion_sort_asc_with_while (A, 6); //Insertion_sort_des (A, 6);MergeSorting2 (A,0,5); return 0;}View Code
Results:
Introduction to Algorithms Notes (II.) Two-way merge sort