Objective
Imagine a reality scene, a few people playing poker, in the process of taking the card, assuming that the number of cards in accordance with the size of the cards, assuming that the hands have a number of cards (in accordance with the size of the cards in order), then the next time we receive the card, we will have just received the card into the hands of the cards in the In reality, this "card" mentality is what we are going to say today, "inserting ideas"
Direct insertion Sorting algorithm
Given a sequence of n-length L, how to use the "card" idea to make it good order? We know that the sorting algorithm involves two basic operations: comparing and moving elements. If sequence A is an array, we take the "in-place sort" (in-place sort), which always regards sequence A as a two-part sorted+unsorted L[0,r) +l[r,n), when initialized | s|=r=0 The empty sequence doesn't matter in order. In the iterative process, focus and process e=l[r], determine the proper position in S (Find in ordered sequence), insert E, get ordered sequence l[0,r].
Invariance: With the increment of R, the l[0,r is always orderly, until R=n,l is ordered as a whole.
Monotonicity: Initially, the problem scale is n, that is, the disordered interval length is n, the ordered interval length is 0, with the increment of R, the ordered sequence expands to the right, and the disordered sequence interval shrinks accordingly (reducing the method strategy) until the final whole order, satisfies the monotonicity
Code implementation
- void insertionsort (int a[], int lo, int hi)//Insert sort A[lo,hi)
- {
- for (int i = lo + 1; i < hi; ++i) //unordered sequence takes the first element each time into an ordered sequence
- {
- int e = a[i]; Ordered sequence a[lo,i), unordered sequence A[i,hi), take a[i] insert an orderly sequence of suitable positions
- int j = i-1;
- for (; J >= Lo;--j)//starting with the element A[i-1] at the end of an ordered sequence, step forward to compare elements with E
- {
- if (A[j] > E) //Scroll an array to move the element larger than e one unit to the right
- A[j + 1] = A[j];
- else//To find the first element not less than the maximum position of E, the end loop
- break;
- }
- A[j+1] = e; Note that position j is the maximum rank of the non-treatment e, so the insertion element amount must be after J
- }
- }
Analysis
The best case of the algorithm, to order all the elements ordered, so just compare n-1 times, after each comparison, to insert the element e is still in the original position, time complexity O (n), the worst case of the algorithm, to order the elements are all reversed, so that when inserting elements, need to compare 1+2+3+4+...+n-1=, Also moves the element number of times, so that the time complexity of O (n2), the average complexity is O (n2)
Since the insertion element position in the sorting process is still in order to satisfy the insertion, that is, the strict meaning is greater than that, so the insertion sort algorithm is a stable sorting algorithm.
Run results
For ease of understanding, the two steps of finding a position in an ordered vector that is not greater than element E and inserting element e are respectively programmed into functions.
Code Implementation 2
- void insertionsort_b (int a[], int lo, int hi)//a[lo,hi) Insert Sort
- {
- for (int I=lo+1;i < hi;++i)
- {
- int e = a[i];
- int index = search (A, E, lo, i); ///in A[lo,i) find element subscript not greater than e
- for (int j = i-1; j > Index; j--)//move a[index+1,i element to the right one cell a (Index+1,i]
- A[j + 1] = A[j];
- A[index + 1] = e; //Insert element e
- }
- }
- int search (int a[], int e, int lo, int hi)//in Vector A[lo,hi) find not greater than the maximum rank of element E and return
- {
- while (--hi >= Lo)
- if (A[hi] <= e)//so that if more than one element is equal to E, the maximum rank is returned
- return hi;
- return hi;//at this time hi=lo-1, that is, the order interval crossed out
- }
Explore & Delve
We see that the time complexity of insertion sequencing is not only related to the size n of the problem, but also to the total number I in the reverse order of the input sequence, and the input-sensitive algorithm input-sensitive has a special position in the sorting algorithm. From the above ordered vector lookup process, we can see that the insert (search (a,e) +1,e) This statement will still maintain ordered vector A, because our search each return is not greater than the maximum rank of E, even if there are more than one repeating element e in an ordered vector, The maximum rank of E is still returned, so that the insertion does not break the order.
Innovative thinking
Since vector A is ordered, do we use the dichotomy method to find the results more quickly? The binary lookup makes the average time performance of the Find success as O (Logn) much higher than the O (n) Order lookup, and next I will explore the two-part search mystery of ordered sequences!
Sort the insertion sort algorithm