The idea of merge sorting is to merge each contiguous two digits of a sequence (merge) to form a floor (N/2) sequence, after which each sequence contains two elements that merge the above sequence again, forming a floor (N/4) sequence, each containing four elements repeating step 2, Until all the elements have been sorted.
The following code is a single-merge function that can be used as a template in your code.
PS: Wherein the preceding paragraph is p-bit to the Q-bit, the latter section is the q+1 bit to the R-position.
1 voidMerge (intPintQintR) {2 intN1 = Q-p +1;3 intN2 = R-Q;4 Long LongL[N1],R[N2];5 for(inti =0; i < n1;i++) L[i] = a[i+p];6 for(inti =0; i < n2;i++) R[i] = a[i+q+1];7L[N1] =100000000; R[N2] =100000000;8 inti =0;intj =0;9 for(intK = p;k <= r;k++){Ten if(L[i] < r[j]) A[k] = l[i++]; One ElseA[k] = r[j++]; A } -}
Merge sort from zero-start learning algorithm [1] (2.2 merge sort)