The merging sort algorithm is a sort algorithm based on the thought of dividing and administering, the Division and administration can be simply understood as "divide and conquer", and the core idea is to divide the complex problem into several sub-problems, so that these sub-problems are the smaller instances of the original problem; then solve these simpler sub-problems, Finally, the results are combined to get the answers to the most complex questions at the beginning.
The idea of merging sorts is the same, as follows:
One, split an array into two parts, generally take the array length of the general position as bounded, divided into two sub-arrays, and then respectively, the two sub-arrays recursively merge sort
Second, we are sure that after the recursive ordering, the original two arrays will be respectively sorted into two arrays, and then we are to merge the two sub-arrays into an array of operations; for the top level, this is the last step to complete the sort operation
thirdly, for each sub-array decomposed, the merging operation.
as long as the length of the array is greater than 1, that is, the ordinal of the head element of the array < The ordinal of the tail element of the subarray, then continue to recursively split into smaller two sub-arrays, and then continue to merge sort ... Always recursively to a time of splitting, two sub-arrays have one array with only one element. Only one element, of course, does not need to be sorted, so the array is merged, because the merging is based on the order of the elements of the two sub-arrays are merged, it is guaranteed to the bottom of the two sub-arrays merge (here are two sub-arrays, at least one is the unit prime Group , and the other array is a unit prime group or a two-element array, and the merged array is given in order, so that the resulting array is a sequential version of the original array, recursively merging up and down the layer.
diagram:
Perhaps not quite understood here, you can look at the figure, we have a more easy to see the process of the 8 elements of the array as an example, the bottom of the group has been split into elements of the original array:
Let's take a step-by-step look at it.
The core of the algorithm is actually how to combine the two arrays in a simple operation. We can simply consider two arrays as two stacks of poker, as an example from small to large.
Each time we take a card from two arrays to compare, put the small one in the third heap (the first empty) heap, and then throw the big card back to its original pile, and then repeat the operation until there is a stack of cards to be evacuated. At this time another pile of cards at least one, and because the two stacks of cards originally from small to large order, so we just need to put the remaining cards of that pile of cards simply moved to the third pile of cards, so that the completion of all the cards from small to large sort of work.
Look at the code:
Merge two arrays, by small to large, the two arrays have been sorted well/* * Merge two arrays */void mergeArr2 (int a[], int n, int b[], int m, int c[]) {int i = 0;//a array Int j = 0; b array int k = 0;while (i < n && J < m) {if (A[i] < b[j]) {c[k++] = a[i++];} else {c[k++] = b[j++];}} while (I < n) {c[k++] = a[i++];} while (J < m) {c[k++] = b[j++];}}
after the above code is sorted, the C array becomes the result of the merging of A and B arrays and the order of the rows.
But it might be a bit inappropriate to write here, because what we got was an array, assuming int a[], and it would be a bit of a hassle if we had to split it into two arrays each time. We can change it a little bit, pass in an array a, and arrays C, We then pass in the first and second ordinal of a and the ordinal of a two array, and the code changes to look like this:
/* * Merge two parts of an array: [Start,mid] and [Mid+1,end]; where start and end are the subscripts of the first and last elements, mid is the middle subscript due to mid= (start+end)/2, * and Start<end , so mid may be equal to start, but must be less than end */void Mergearr (int a[], int start, int mid, int end, int*tmp) {int i = Start;int J = mid + 1;i NT k = 0;while (i <= mid && J <= end) {if (A[i] < a[j]) {tmp[k++] = a[i++];} else {tmp[k++] = a[j++];}} while (I <= mid) {tmp[k++] = a[i++];} while (j <= end) {tmp[k++] = a[j++];} cout << "k=" << k << Endl;}
Although the above code completes the merge, there is a small problem, that is, we generally hope that after the merger will be the original int a[] array is merged and ordered, not the TMP array, so here we also need to add a paragraph above the end of the code, Copy the number in TMP to the location corresponding to the A array:
void Mergearr (int* A, int start, int mid, int end, int*tmp) {int i = Start;int J = Mid + 1;int k = 0;while (i <= mid &A mp;& J <= End) {if (A[i] < a[j]) {tmp[k++] = a[i++];} else {tmp[k++] = a[j++];}} while (I <= mid) {tmp[k++] = a[i++];} while (j <= end) {tmp[k++] = a[j++];} cout << "k=" << K << endl;//so far, TMP merges the contents of the two-piece array in a, copying it, but our goal is to have the data in a in a sequence and put it in order, so there's a job to be done, Copy array from TMP for (int i = 0; i < K; i++) {A[start + i] = Tmp[i];}}
In this way, the code for the merge operation is complete, we just need to follow the previous idea, write the merge sort function:
/* * Merge sort */void mergesort (int a[], int start, int end, int* tmp) {//start < end condition allows sub-array length equal to 1 o'clock, no longer merge sort if (Start < E nd) {int mid = (start + end)/2;mergesort (A, start, Mid, TMP), MergeSort (A, mid + 1, end, TMP); Mergearr (A, start, Mid, end , TMP);}}
at this point, the merge sort is complete.
We can take a simple look at the time complexity of the merge sorting algorithm, and for the merging operation, the number of cycles per merge function is end-start+1.
This is still not very clear, we might as well assume that the original array length n is 2 power, and then decomposed to the last layer when the object of the merge operation is two units of Prime Group, then a merge operation needs to go through two cycles, the last layer has N/2 times the merge operation, so need to go through n /2*2=n Cycles; For the second-to-last level, each merge operation object is two two-element array, a merge operation needs to go through 4 cycles, this layer needs to go through the N/4-times merge operation, so it needs to go through n/4*4=n cycles ... And so on, each layer needs to go through n cycles, and how many layers are there? We start with n number, each layer is divided by 2, has been in addition to the last layer equals 1 arrays, assuming that there is a K-layer, so 2 K-square =n, so K=logn layer (2 is the bottom), so there is a total need to n*logn cycles, that is, the algorithm time complexity. Of course this ignores some of the middle constants.
Look at the diagram below to make it clearer:
Algorithm base four (merge sort)