Sort algorithm--Hill sort

Source: Internet
Author: User

The hill sort is an upgraded version of the Insert Sort, which works by comparing elements that are spaced apart; the distance used for each comparison decreases as the algorithm progresses until the last order of the adjacent elements is compared. For this reason, the hill sort is also known as narrowing the incremental sort

Hill sort uses a sequence h1,h2,h3,...., HK, called an incremental sequence. As long as the h1=1, any increment sequence is feasible. After a trip of the incremental HK, for each i we have a[i]<=a[i+hk]; all elements separated by HK are sorted. At this point the file is hk-sorted.

For example:


(in 5-sort, that is, the 0,5,10 element is a set of sub-sequences: 81,35,41 is the insertion sort, which is: 35,41,81. The same can be achieved by other 5-sorted sequences and other hk-sort)

That's a trip. The function of hk-sort is to perform one-time insertion sort on HK independent sub-arrays;

Algorithm implementation:

#include <stdio.h> #define MAX 13void shellsort (int *data,int N); int main () {    int data[max]={ 81,94,11,96,12,35,17,95,28,58,41,75,15};    int i=0;    printf ("Before sort:\n");    for (i=0;i<max;i++)    {        printf ("%d\t", Data[i]);    }    Shellsort (Data,max);    printf ("\nafter sort:\n");    for (i=0;i<max;i++)    {        printf ("%d\t", Data[i]);    }    return 0;} void Shellsort (int *data,int N) {    int incre=0;//increment    int i=0,j=0;    int tmp;    for (incre=n/2;incre>0;incre/=2)    {for        (i=incre;i<n;i++)        {            tmp=data[i];            for (J=i;j>=incre;j-=incre)//each time it is compared to the number of previous j-incre, the iteration goes down each iteration is an embodiment of the insertion sort            {                if (Tmp<data[j-incre])                    Data[j]=data[j-incre];                Else                    break;//indicates sorted            }            data[j]=tmp;    }}}



Sort algorithm--Hill sort

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.