**Insert Merge**

The time complexity of merge sort is O (NLGN), and the space complexity is O (n).

In general, however, based on the 22 merge from a single record is not particularly advocated, a more commonly used improvement is to combine the insertion sort, that is, the insertion sort to obtain a longer ordered subsequence, and then 22 merge (the improved merge is also stable, because the insertion sort is stable). There is a reason for this improvement: although the worst case of the insertion sort is O (n^2), which appears to be greater than the worst case O (NLGN) of the merge, usually, because the constant factor of the insertion sort makes it run faster in the case of n smaller, when the sub-problem is sufficient, It is more appropriate to use an insertion sort.

**Analysis of complexity**

The following analysis inserts a merge sort in the worst case of complexity: assuming that the entire sequence length is n, when the subsequence length is k, the insertion sort strategy is taken, such that a total of n/k subsequence.

The subsequence completes the sorting complexity: In the worst case, the time complexity of the n/k sub-sequence completion is O (NK). Proof: Each sub-sequence is completed insert sorting complexity is O (k^2), altogether n/k subsequence, therefore O (NK).

Subsequence Completion Merge complexity: In the worst case, the time complexity of merging two sub-sequences is O (n), there are altogether n/k Subsequence, 22 merging, total need of LG (N/K) step merging, so the complexity of the completion of these sub-sequences is O (NLG (n/k)).

So the worst case complexity of the improved insert merge sort is O (NK+NLG (n/k)), where the maximum value of K cannot exceed lgn, obviously if K is greater than LGN, complexity is not merged with a level, that is, assuming a 1000-length array, using the insertion strategy to sort the sub-sequence, The maximum length of a subsequence cannot exceed 10.

/* Two-way merge sort */void merge (int* array,int low,int middle,int high) {int* temp = new int[sizeof (int) * (high-low+1)];int first = Lo W;int second = Middle+1;int I=0;while (first<=middle&&second<=high) {if (Array[first] < Array[second]) {temp[i++] = array[first++];} else{temp[i++] = array[second++];}} while (First<=middle) {temp[i++] = array[first++];} while (Second<=high) {temp[i++] = array[second++];} memcpy (array+low,temp,sizeof (int) * (high-low+1));d elete [] temp;} void MergeSort (int* array,int begin,int end) {//cout<<sizeof (array) <<endl;if ((end-begin) >0) {int mid = Begin + (End-begin)/2; MergeSort (Array,begin,mid); MergeSort (Array,mid+1,end); Merge (Array,begin,mid,end);}} /* Improved merge algorithm: Insert merge * first by inserting sorting to get a long ordered string, and then merge * that is, when the decomposed array length is less than a certain value, no longer decompose, instead of inserting sort */ #define Insert_bound 5 void Insertsort (int arr[], int beg, int end) { for (int i = beg+1; I <= end; ++i) {&N bsp; int temp = arr[i]; Int J = i-1; while (j >= Beg && arr [j] > Temp) { arr[j+1] = arr[j--]; } Arr[j+1] = temp; }} void insert_mergesort (int arr[], int beg, int end, int temp_arr[]) {  ; if (End-beg + 1 <= insert_bound) { Insertsort (arr,beg,end); }else { int mid = (beg + end)/2; Insert_mergesort (arr, Beg, Mid, Temp_arr); &nbs p; Insert_mergesort (arr, mid+1, end, Temp_arr); Merge (arr, Beg, Mid, End, Temp_arr); }}

**Merge in situ**

We say that the merge sort requires an extra space of O (n) relative to the queue, which is once a drawback of merging, but fortunately the merge sort can also be sorted in place, using only O (1) of extra space. The core idea used in the in-situ merge sort is the variant of "reverse memory", that is, "exchanging two contiguous blocks of memory", and in the article "thinking about inverse string (Reverse Words) and three kinds of solution" in the paper, the article analyses the question of a face. This idea is used in a lot of places, in the "Programming Zhu Ji Nanxiong" is called "hand-crank algorithm." By hand algorithm exchange memory idea to do in-situ merger and there are many variants, we have an example of a more common situation, different methods and based on binary search method to determine the exchange of memory block, in the "computer programming art" also has different ideas to provide, interested in this article reference.

The following is an example of the idea of in-situ merge sorting.

We know that, whether it is based on a single record of 22 merges, or using the insertion sort to get a longer subsequence and then merge, in the process of algorithm merging, we are merging "two contiguous ordered subsequence".

Before understanding the idea of merging in situ, recall the general merging algorithm, first put the ordered subsequence into a temporary array, and then set two pointers from the beginning of two sub-sequences to find the smallest element into the merge array; so is **the idea of merging in place. To ensure that the number before the pointer is always the smallest element in the two subsequence** . Words are more useless, see examples of illustrations, one can see.

Let's say we now have two ordered subsequence a, and we're going to start with plot example B for in-place merging

b, first the value of the first subsequence is compared to the first value 20 of the second subsequence, if the value of the sequence one is less than 20, then the pointer I moves backward until a value greater than 20 is found, that is, the pointer i is moved to five, **and after B we know that the value before pointer I must be the smallest block in the two subsequence **

c, a temporary pointer is used to record the position of J, and then the second sub-sequence of the value of the sequence I refers to a value of 30 compared, if the value of sequence two is less than 30, then J-shift, until a value greater than 30 is found, that is, J moves to 55 subscript;

D, through the process of Figure C, we know that the values in the array block **[index, J]** must be all less than the value 30 referred to by pointer I, that is, the values in the array block **[index, J]** are all less than the values in the array block [ **I, index]** , To satisfy **the principle of in-place merging: always ensure that the element before pointer i is the smallest element in the two sequence, that is, before I is a well-merged element. **we swap the memory blocks of these two arrays, and after swapping I move the corresponding steps, this "step" is actually the number of values that the step is merged into, that is, the number of array blocks **[index, J]** . Thus get figure E as follows:

Repeating the above process, F, is equivalent to the process of Figure B, until finally, this is an implementation of the idea of in-situ merging, the specific code is as follows.

void Revere (int* array,int begin,int end) {int temp;while (begin <end) {temp = Array[begin];array[begin] = Array[end]; array[end]=temp;begin++;end--;}} /*array is the starting position of the array begin array with rotation middle for the second half of the beginning end for the second half of the beginning */void rotate_right (int* array,int begin,int middle,int end) {Reve Re (array,begin,middle); Revere (Array,middle+1,end); Revere (Array,begin,end); } void Merge_second (int* array,int begin,int end) {int middle = begin + (end-begin)/2+1;int i = Begin;int index;while (middl E <= End && I<middle) {while (Array[i]<=array[middle] && i<middle) {i++;} index = middle;while (middle <=end && Array[middle]<=array[i]) {middle++;} Rotate_right (array,i,index-1,middle-1); i+= (Middle-index);}} void Inplace_mergesort (int arr[], int beg, int end) { if (Beg < end) { & nbsp; int mid = (beg + end)/2; Inplace_mergesort ( Arr, beg, mid); Inplace_mergesort (arr, mid+1, end); Merge_second (arr, beg, end); }}

For in-situ merge, it is only a kind of merging means, in order to achieve the merge sort, can be used in the two-way merge in-situ merge, can also be used in the merge algorithm based on the improved insertion sort, as shown in the code.

If there are two sequential sequences within an array then how to merge the two ordered sequences can be used

Problem Description:

Arrays al[0,mid-1] and al[mid,num-1] are each ordered, the two sub-ordered segments of the array al[0,num-1] are merge,

Get al[0,num-1] overall orderly. Requires a space complexity of O (1). Note: The al[i] element is supported by the ' < ' operator.

/* Idea: combine two sequential sequences in the same array into one that can be sorted using an INSERT, insert the elements of the second half into the first half of each element of the second half to find a suitable position in the first half, and then insert this to move the first half of the elements back one position */void Merge (int* array,int begin,int end) {int middle = begin + (end-begin)/2+1; int I=begin;int temp;while (middle <=end) {Temp = Array[middle];if (Array[i] < Array[middle]) {i++;} Else{int index = middle;while (Index! = i) {Array[index] = array[index-1];index--;} array[i++] = temp;middle++;}} }

Of course, you can also use the Merge_second function above, which is also the method of merging ordered sequences in situ

Reference: http://www.ahathinking.com/archives/103.html

Two-way merge && Insert Merge && in-situ merge