Sorting algorithm - direct insertion sorting (super detailed graphics!)

Introduction

English name: Straight Insertion Sort
is also the simplest sorting method. Its basic operation is to insert a record into the sorted list to obtain a new sorted list with the number of records increased by 1.

step

The following uses the array 2,5,8,3,6,9,1,4,7 as an example
to sort from small to large

1. First look at the first number and divide the array into ordered and unordered parts
  • First look at the first number 2, a number must be ordered, so divide 2 into order, and the rest are disordered
    insert image description here
2. The first insertion of the unordered part into the ordered part
  • Take out the first part of the unordered part, compare it from back to front in the ordered part, and insert it into the appropriate position
    insert image description here
    insert image description here
3. Repeat step 2 until all unordered parts are inserted into order
  • 8 is also a comparison that can be inserted
    insert image description here
    insert image description here

  • 3 requires multiple comparisons, note that it is multiple comparisons, direct insertion, not one comparison and one insertion (different from bubbling)
    insert image description hereinsert image description here

The following steps are also very simple, no longer given

the code

  • From the above process, it can be seen that this algorithm traverses all numbers once and inserts them separately, but the first number must be in order and does not need to be sorted, so n numbers need n-1 traversal
  • That is, i starts directly from 1
  • The comparison of each insertion starts from the previous number, so we can directly set the parameter j of the second loop to i-1
    - the second time is
  • For each insertion, we first take out the number to be inserted
    insert image description here
  • Then compare, if it is greater than the number taken out, move the number back, j-1
  • insert image description here
    insert image description here- then compare
    insert image description here
  • At this time, compare again, and there is a number less than 3, so jump out of the loop, 3 can be inserted into the array, and the subscript is j+1

insert image description here

  • Another way to jump out of the loop is to go to the front and still not have a number smaller than the fetched number. At this time, jump out of the loop directly. Take 1 as an example

insert image description here- The next step is to move backward according to the rule 2, j-1, at this time j=-1, and jump out of the loop, 1 is placed at the position of subscript j+1, which is the first one, which is consistent with the previous one

insert image description here

  • To sum up, we need to restrict j>=0 and temp<a[j], and finally a[j+1]=temp

  • Optimization: When the number to be inserted is already greater than the previous number, there is no need to take it out and put it in again

#include<bits/stdc++.h>

using namespace std;

void InsertSort(int a[],int l)
{
    
    
    int temp;
    int j;
    for(int i=1;i<l;i++)
    {
    
    
        if(a[i]<a[i-1])
        {
    
    
            temp=a[i];
            for(j=i-1;j>=0&&temp<a[j];j--)
            {
    
    
                a[j+1]=a[j];
            }
            a[j+1]=temp;

        }
        for(int k=0;k<l;k++)
            cout<<a[k]<<" ";
        cout<<endl;

    }
}


int main()
{
    
    
    int a[10]={
    
    2,5,8,3,6,9,1,4,7};
    int b[10]={
    
    1,2,3,4,5,6,7,8,9};
    int len=9;
    InsertSort(a,len);
    return 0;
}


characteristic

1. Time complexity

The best case is that all are in order, and only one traversal is needed at this time, and the best time complexity is O ( n ) O(n)O ( n )
In the worst case, all are reversed, and the inner layer traverses the sorted part each time. The worst time complexity isO ( n 2 ) O(n^2)O ( n2)

In summary, the average time complexity of direct insertion sorting is O ( n 2 ) O(n^2)O ( n2)

2. Space complexity

Auxiliary space is constant
Average space complexity is: O ( 1 ) O(1)O(1)

3. Algorithm stability

Whether the order of the same elements is changed
insert image description here

Inserted in front of the number larger than it, so the direct insertion sort is stable

small test

  • In the above code, the printed array is like this
    insert image description here

  • Can you change the code so that the ordered array is traversed in the back, i.e. from back to front?
    insert image description here

New algorithm, old routine, do you want to play a new trick next time? (≖ᴗ≖)✧

Guess you like

Origin blog.csdn.net/qq_44616044/article/details/115708056