/*********************************************************************************************** 1. Set two pointers, The initial position is the starting position of the two sorted series 2. 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 3. Repeat steps 3 until a pointer reaches the end of the sequence 4. Copy all remaining elements of another sequence directly to the merge sequence footer merge sort: Merge sort works as follows (assuming that the sequence has n elements): 1. Merges each contiguous two digits of the sequence to form a floor (N/2) sequence, with each sequence containing two element 2. Merge the above sequence into a sequence of floor (N/4) Each sequence contains four elements 3. Repeat step 2 until all elements are sorted the merge sort is stable, its worst, average, and the best time is O (Nlogn). However, it requires additional storage space. Why ask? hovertree.com Merge Sort method (merge sort, hereinafter referred to as MS) is a model for the application of the idea of division and administration. The main algorithm operation can be divided into the following steps: Step 1: Divide n elements into two sub-sequences with N/2 elements Step 2: Recursively sort two sub-sequences with MS (finally, the entire original sequence can be decomposed into n subsequence) Step 3: Merge two sorted sequences ************* ***********************************************************************************/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<climits>#include<cstdlib>#include<time.h>#include<cstdlib>#include<cstdio>using namespacestd; voidRandom (intA[],intN) {intI=0; Srand ((unsigned) time (NULL)); while(i<N) {a[i++]=rand (); } } voidMergeint*a,intLowintMidintHigh//merge Operations{ intK, Begin1, Begin2, End1, End2; Begin1=Low ; End1=mid; Begin2= Mid +1; End2=High ; int*temp = (int*)malloc((High-Low +1) *sizeof(int)); for(k =0; Begin1 <= end1 && begin2 <= end2; k++)//from childhood to the big sort { if(A[begin1] <=a[begin2]) temp[k]= a[begin1++]; ElseTemp[k]= a[begin2++]; } if(Begin1 <= End1)//left remainingmemcpy (temp + k, A + begin1, (end1-begin1 +1) *sizeof(int)); Else //left Rightmemcpy (temp + k, A + begin2, (end2-begin2 +1) *sizeof(int)); memcpy (A+ Low, temp, (high-low +1) *sizeof(int));//copy to original array after sorting Free(temp);//Free Space} voidMerge_sort (int*a, unsignedintBegin, unsignedintend) { intmid; if(Begin <end) {Mid=begin+ (End-begin) >>1; //mid = (end + begin)/2; Prevent data addition overflowMerge_sort (A, begin, mid);//Divide and conquerMerge_sort (A, Mid +1, end);//Divide and conquerMerge (A, begin, Mid, end);//Merge two sorted columns } } intMain () {inta[ -]; Random (A, -); for(intI=0;i< -; i++) {cout<<" "<<a[i]<<" "; } merge_sort (A,0, --1); for(intI=0;i< -; i++) {cout<<" "<<a[i]<<Endl; } return 0; }
Recommendation: http://www.cnblogs.com/roucheng/p/cppjy.html
Implementation of two-way merge sorting algorithm-complete C language Program