Merging algorithm uses the concept of binary tree to realize sorting algorithm, and establishes the algorithm based on recursive merging operation. The algorithm is implemented by grouping the arrays into two sequences, sorting them, and then sorting them in a merge order. The merging sort algorithm is a sort algorithm which is realized by the idea of merging.
First, the algorithm thought
Array sequence {16, 7, 13, 10, 9, 15, 3, 2, 5, 8, 12, 1, 11, 4, 6, 14}, with a certain sort of exchange, get ordered small sequence, after 22 merge sort and then merge, finally get an ordered array. From the shape, it looks like an inverted full binary tree.
Its principle is that the initial sequence contains n records, it can be regarded as n ordered sub-sequence, each subsequence length is 1, and then 22 is merged, to get |n/2| (|x| means the smallest integer not less than x) of 2 or 1 of the ordered subsequence; then 22 merges, ..., so repeat, Until a sequential sequence of length n is obtained, this sort method is called a 2-way merge sort.
Merge sort actually two things to do:
(1) "decomposition"--divides the sequence binary each time.
(2) "Merge"--sorts after the divided sequence segment 22 is merged .
Second, the Code
//Merge SortInlinevoidSortalgorithm::mergesort (int*plarray,int*prarray,intStart,intlength) { //because it is a two-fork tree concept, the length is general, the span of the merger is the step inti =1; intJ; while(I <= Length-2*start +1) {Merge (plarray,prarray,i,i+start-1, i+2*start-1);//22 Mergingi = i+2*start; } if(I < length-start+1)//Merge The last two sequences, here is length, note the point{Merge (plarray,prarray,i,i+start-1, length); } Else for(j = i;j<= length;j++)//if the last sequence is leftPRARRAY[J] =plarray[j];} InlinevoidSortalgorithm::merge (int*plaray,int*prarray,intStartintLength1,intlength2) { intj,k, L; for(k = Start,j = length1+1; start<=length1&&j<=length2;k++)//The group merges the corresponding elements { //the number on the left is less than the right, and the small number is filled into the target array if(Plaray[start] <Plaray[j]) {Prarray[k]=Plaray[start]; Start++; } Else{Prarray[k]=Plaray[j]; J++; } } if(Start <=length1)//fill in the remaining fields into the target array { for(L =0; l<=length1-start;l++) {prarray[l+k] = plaray[start+L]; } } if(J <=length2) { for(L =0; L <= length2-j; l++) {prarray[l+K] = plaray[j+l];//copy the remaining SR[J...N] to tr } }}
//Merge SortvoidSortalgorithm::mergesort (psqllist pList) {//application space, storing the merged ordered array int*array =New int[Ten]; memset (Array,0,plist->length); intK =1; while(K < plist->length) { //merges the sequence, starting with the sequence size of 1, and then 2,4,8,MergeSort (plist->sqlarray,array,k,plist->length-1); K= k*2; MergeSort (Array,plist->sqlarray,k,plist->length-1); K= k*2; for(inti =0; i<plist->length;i++) {printf ("%d\t", Array[i]); } printsqllist (PList); printf ("\ n"); }}
Third, Code Analysis
The results of the program, such as the analysis of the following two pictures
Steps:
1, the first is 22 to exchange the merger, form the preliminary ordered small sequence
2. Then the 22 ordered sequence is merged again, forming four groups, in which the key code is in the merge function.
3, the program must pay attention to the beginning and end of the array, as well as the array length problems, for example, this array is 9 data, and finally 8 elements and 1 elements of the merge, after the call merge, the length.
On data structure-merge sort