Sorting algorithm - half insertion sort (advanced!)

Introduction

First of all, we must understand direct insertion sort
and binary search

English name: binary insertion sort
is an improved algorithm based on the direct insertion sort algorithm

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. Find the insertion position
  • Take out the first part of the unordered part, and find the position in the ordered part

  • 2,5,8 do not need to move, so start directly from the insertion of 3

  • Or take out the number to be inserted first
    insert image description here

  • Then use binary search to find the position that should be inserted
    insert image description here

3. Move the array, and finally insert the number taken out
  • Because the position has been found, so move all directly, no need to compare one by one

insert image description here

  • insert
    insert image description here

The following steps are similar to direct insertion, and will not be given

the code

  • The difference between half insertion and direct insertion lies in the method of finding the insertion position
  • Because it is a binary search, low and high are also introduced and set as the boundary of the ordered area
    insert image description here
  • When this number does not exist in the ordered part, according to the inference of dichotomy, the final low and high should be like this
    insert image description here
  • When there is such a number, what is the result?
  • Note that in the code, when low and high are the same, the change is low
    insert image description here
  • It can be seen that they all start to move from low, and finally put them into the position pointed by low
#include<bits/stdc++.h>

using namespace std;

void BInsertSort(int a[],int l)
{
    
    
    int temp;
    int low,high;
    int m;
    for(int i=1;i<l;i++)
    {
    
    
        if(a[i]<a[i-1])
        {
    
    
            low=0;
            high=i-1;
            while(low<=high)
            {
    
    
                m=low+(high-low)/2;
                if(a[m]>a[i])
                    high=m-1;
                else
                    low=m+1;
            }
            temp=a[i];
            for(int j=i;j>low;j--)
                a[j]=a[j-1];
            a[low]=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;
    BInsertSort(a,len);
    return 0;
}


characteristic

1. Time complexity

The half search only reduces the number of comparisons, but the number of moves of elements remains the same, so the time complexity is O ( n 2 ) O(n^2)O ( n2)

2. Space complexity

The average space complexity is also: O ( 1 ) O(1)O(1)

3. Algorithm stability

Whether the order of the same elements is changed has been mentioned in the above description, let’s take an example to look at the
array 2 4 5 8 3 6 4 1 4
output low every time, that is, the value of the inserted position
insert image description here

insert image description here

  • It can be seen that the same number in the back will only be inserted after the same number, so it is still a stable sorting algorithm

small test

The content is a bit too much, so I won’t do this. If you want to change it, it’s still the same as the old rules. It’s the same as direct insertion
٩( 'ω' )و Crab!

Guess you like

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