Sort the insertion sort algorithm

Source: Internet
Author: User

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
  1. void insertionsort (int a[], int lo, int hi)//Insert sort A[lo,hi)
  2. {
  3. for (int i = lo + 1; i < hi; ++i) //unordered sequence takes the first element each time into an ordered sequence
  4. {
  5. int e = a[i]; Ordered sequence a[lo,i), unordered sequence A[i,hi), take a[i] insert an orderly sequence of suitable positions
  6. int j = i-1;
  7. 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
  8. {
  9. if (A[j] > E) //Scroll an array to move the element larger than e one unit to the right
  10. A[j + 1] = A[j];
  11. else//To find the first element not less than the maximum position of E, the end loop
  12. break;
  13. }
  14. 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
  15. }
  16. }

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
  1. void insertionsort_b (int a[], int lo, int hi)//a[lo,hi) Insert Sort
  2. {
  3. for (int I=lo+1;i < hi;++i)
  4. {
  5. int e = a[i];
  6. int index = search (A, E, lo, i); ///in A[lo,i) find element subscript not greater than e
  7. for (int j = i-1; j > Index; j--)//move a[index+1,i element to the right one cell a (Index+1,i]
  8. A[j + 1] = A[j];
  9. A[index + 1] = e; //Insert element e
  10. }
  11. }
  12. 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
  13. {
  14. while (--hi >= Lo)
  15. if (A[hi] <= e)//so that if more than one element is equal to E, the maximum rank is returned
  16. return hi;
  17. return hi;//at this time hi=lo-1, that is, the order interval crossed out
  18. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.