Thought map from: https://www.cnblogs.com/fanwencong/p/5910503.html
Here we still use the sequential table to implement this sort algorithm.
The order table is the same as the number No. 0 bit unused.
Here our merge sort is two way merge, the idea is that the sequence has been decomposed into two parts, until the length of the sub-sequence is 1, then it is clear that the sub-sequence has been ordered, and then continue to merge the ordered sequence, the final implementation of sorting.
The following code:
First, the interface and declaration of the sequential table:
#defineOVERFLOW 0#defineERROR 0#defineFALSE 0#defineOK 1#defineTRUE 1typedefstruct { int* ELEM;//Base Address Space intLength//Current Length intSize//Storage Capacity intIncrement//Increment of expansion}sqlist;/*interfaces for sequential tables*/intInitsqlist (SqList &l,intSizeintINC);//Initialization Order TableintDestroysqlist (SqList &l);//Destroy Order table LintClearsqlist (SqList &l);//Empty the Order tableintSqlistisempty (SqList L);//empty functionintGetelemsqlist (SqList L,intIint&E);//return the order form L element I with EintSearchsqlist (SqList L,inte);//finds element E in the sequential table, returns the first occurrence of the element if successful, or 1intPutelemsqlist (SqList &l,intIinte);//change the I element in L to eintAppendsqlist (SqList &l,inte);//add element E in L tableintDeletelastsqlist (SqList &l,int&E);//Delete the tail element of the L table and use the parameter e as the return valuevoidTraversesqlist (SqList L);//iterate through the elements and print /*random number generation function*/int* Randomgenerate (intn);//generate an array of n random numbers /*test function*/
Look at the algorithm:
voidMerge (SqList & L1,sqlist L2,intIintMintn);//The merging operation of the 2-way merging L1 the adjacent ordered interval i->m and m+1->n into I->n ordered sequences to be stored in L2. voidMsort (sqlist L1, SqList L2,intIintSintT);//Recursive sort operation of recursion merge sort, if I is odd, the sorted records are stored in L2 otherwise saved into L1intMain () {srand (unsigned) time (NULL));//add a sentence of Srand ((unsigned) time (NULL)); Turn on random triggers and clock frequency synchronizationsqlist L; Initsqlist (L,6,5);//add one more memory by default int* Testarray = Randomgenerate (6); inti; for(i =0; I <6; i++) {appendsqlist (L, Testarray[i]);//The default number starts with 1th digits} printf ("The original list:\n"); Traversesqlist (l); //Sorting module:SqList L2; Initsqlist (L2,6,5);//Secondary Order tableMsort (L, L2,0,1, l.length);//because it is the row order table L, so the incoming I is even 0Destroysqlist (L2); } voidMerge (SqList & L1, SqList L2,intIintMintN) {//The merging operation of the 2-way merging L1 the adjacent ordered interval i->m and m+1->n into I->n ordered sequences to be stored in L2. intj = i, K = m +1, h =i; for(; J <= m, K <= N; h++) { if(L1.elem[j] <= l1.elem[k]) {//If this doesn't add up, it's an unstable sort.L2.ELEM[H] = l1.elem[j++];//Whichever one is, put it back. } Else{L2.elem[h]= l1.elem[k++]; } } while(J <=m) {l2.elem[h+ +] = l1.elem[j++]; } while(k <=N) {l2.elem[h+ +] = l1.elem[k++]; } }voidMsort (sqlist L1, SqList L2,intIintSintT) {//Recursive sorting operations that recursively merge sorts. Here is the sort of L1 if I is an odd number, the sorted record is stored in the L2, otherwise it will be saved into the L1. intm; if(s = = t) {//End Condition if(i%2==1) {//Odd WordsL2.elem[s] =L1.elem[s]; } } Else{m= (s + t)/2; Msort (L1, L2, I+1, S, m);//recursive ordering of the first half partsMsort (L1, L2, i +1, m+1, t);//recursive ordering of the second half part ifI2==1) {Merge (L1, L2, S, M, T); } Else{Merge (L2, L1, S, M, T); } }}
Recursive implementation of merge sort