[Illustration] Hill sorting of classic algorithm

Before learning Hill sorting, we need to know how the direct insertion sort is sorted

Direct insertion sorting refers to when we finish receiving the test papers and arrange them according to the student number
1. Take the first test paper first
2. Look at the second test paper, put the student number at the back, and put the small student at the front
3. Look at the third test paper, Put it in the corresponding position
4. By analogy, the student number is our number

Hill sorting also uses the same method, with a slight difference

Hill sorting is first 5 sorting by 5 sorting, then 2 sorting by 2, and finally one by one, one by one, and then inserting sorting directly.

Although the principle is similar, Hill sorting is fast, obviously hierarchical filtering, step by step to make the array more and more orderly is faster than direct sorting

Picture from https://www.cnblogs.com/dong-xu/p/7088447.html?utm_source=debugrun&utm_medium=referral
Insert picture description here

#include<stdio.h>
main()
{
    
    
    int a[110],j,i,k,l,d,n;
    scanf("%d",&n);d=n/2;
    for(i=1;i<=n;i++) scanf("%d",&a[i]);
    while(d){
    
    

    for(i=1+d;i<=n;i++)
    {
    
    
        if(a[i]<a[i-d]){
    
    
            a[0]=a[i];
            a[i]=a[i-d];
            for(j=i-2*d;j>0&&a[j]>a[0];j-=d) a[j+d]=a[j];
            a[j+d]=a[0];
        }

    }
    d/=2;
    for(l=1;l<=n;l++) printf("%d ",a[l]);
    printf("\n");
    }
}

Guess you like

Origin blog.csdn.net/qq_43249043/article/details/95885181