Three kinds of insertion sort: direct insertion sort, half insertion sort, hill sort

**

Insertion sort summary

1. Direct insertion sorting
Algorithm features:
1. Stable sorting
2. It is also suitable for chain structure, but there is no need to move records on the singly linked list, just modify the corresponding pointer.
3. It is more suitable for the situation where the initial records are basically in order. When the initial records are out of order and n is large, it should not be used.
4. Time complexity O(n*n), space complexity O(1);

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

void  InsertSort(int *a,int length){
    
    
    int j;
    for(int i=1;i<length;i++){
    
    
           int t=a[i];
        for(j=i-1;j>=0;j--){
    
    
            if(t<a[j]){
    
    
                a[j+1]=a[j];
            }else break;
        }
            a[j+1]=t;
    }
}
int main()
{
    
    

    int a[10]={
    
    49,38,65,97,76,13,27,49,55,04};
   InsertSort(a,10);
    for(int i=0;i<10;i++)
    cout <<a[i]<<" ";
    return 0;
}

2. Binary insertion sort
Algorithm features:
1. Stable sorting
2. Because the binary search is required, it can only be structured in sequence, not chain structure.
3. It is suitable for the situation where the initial record is disordered and n is large.
4. Time complexity o(n), space complexity o(1);

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

void  BInsertSort(int *a,int length){
    
    
    int j;
    for(int i=1;i<length;i++){
    
    
           int t=a[i];
           int low=0,high=i-1;
           while(low<=high){
    
    
            if(t<a[(low+high)/2]) high=(low+high)/2-1;
            else  low=(low+high)/2+1;
           }
           for(j=i-1;j>=low;j--){
    
    
            a[j+1]=a[j];
           }
           a[low]=t;
    }
}
int main()
{
    
    

    int a[10]={
    
    49,38,65,97,176,13,27,49,55,04};
   BInsertSort(a,10);
    for(int i=0;i<10;i++)
    cout <<a[i]<<" ";
    return 0;
}

3. Features of Hill sorting
algorithm:
Jumping movement leads to instability. It
can only be used in sequential structure, not in chain structure.
Incremental array can have multiple options, but the value in the increment should not be other than 1. The common factor of, and the last increment must be 1; the
total number of comparisons and the number of moves is less than that of direct insertion sort, the larger the n, the more obvious the effect. Therefore, it is suitable for the situation where the initial record is disordered and n is large.

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void ShellInsert(int a[],int dt,int length){
    
    
int t,j;
for(int i=dt;i<length;i++)
    if(a[i]<a[i-dt]){
    
    
            t=a[i];
    for(j=i-dt;j>=0&&a[j]>t;j=j-dt)
        a[j+dt]=a[j];
    a[j+dt]=t;

    }
}
void ShellSort(int a[],int dt[],int t,int length){
    
    
    for(int i=0;i<t;i++){
    
    
        ShellInsert(a,dt[i],length);
    }
}
int main()
{
    
    
    int a[10]={
    
    49,38,65,97,76,13,27,49,55,04};
    int dt[3]={
    
    5,3,1};
    ShellSort(a,dt,3,10);
    for(int i=0;i<10;i++)
    cout <<a[i]<<" ";
    return 0;
}

Guess you like

Origin blog.csdn.net/changbaishannefu/article/details/111499439