The Java sort algorithm of hill sort

Source: Internet
Author: User

Hill sort is one of the first algorithms to break the two-time barrier.

it works by comparing the elements that are spaced apart, and the distances used by the comparisons are reduced as the algorithm progresses, until the last trip (comparing adjacent elements). So Hill sort is also called reduced incremental sort .

The hill sort uses a sequence of h1,h2,h3...hk to sort.

Specifically, it means

The First order compares the elements separated by HK, that is, comparing a[i] with A[I+HK], guaranteeing A[I]<=A[I+HK].

The second comparison is the element separated by Hk-1, which is the comparison of A[i] and a[i+hk-1], to ensure a[i]<=a[i+hk-1].

Until the last comparison is between the elements separated by H1:

Thus this sequence is also called an increment sequence , which is also called reducing the increment sort .

Any increment sequence is possible as long as the H1 is equal to 1. (that is, it must be a comparison between adjacent elements)

Specifically, look at the Java algorithm implementation:

 PackageK;ImportJava.util.Scanner;/*** Hill Sort *@authorTangzh **/ Public classShellsort { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); String str[]=in.nextline (). Split ("\\s+"); intinter[]=New int[Str.length];  for(inti=0;i<str.length;i++) {Inter[i]=integer.valueof (Str[i]);                } shellsort (inter);    OutPut (inter); }        /*** Hill Sort *@paramInter*/    Private Static voidShellsort (intinter[]) {         for(intgap=inter.length/2;gap>0;gap/=2) //gap for HK { for(inti=gap;i<inter.length;i++)            {                intj=i; inttemp=Inter[i];  for(; J>=gap && temp<inter[j-gap];j-=Gap) Inter[j]=inter[j-Gap]; INTER[J]=temp; }        }    }        Private Static voidOutPut (int[]inter] {         for(intn:inter) {System.out.printf ("%d", N); }    }}

The way to achieve hill sorting is sellsort (int inter[]);

Next, take a good look at each step of the algorithm:

If the input is: 4 6 7 4 16 (at this time gap=4,i=4,j=4,temp=inter[4]=7)

1.

At this time gap=4

So for the first time 7 and 12 compare, if less than on each other exchange.

I++,12 is compared with 4, which is less than the interchange,

i++, 4 compared with 6, is less than the interchange,

I++,16 is compared with 24, which is less than the interchange.

After the first trip: 7 4 4 6

2.

7 4 4 16 12 12 6 24

At this time gap=2

So for the first time 4 and 7 compare, if less than on each other exchange.

4 4 7 12 12 6 24

I++,16 compared with 4, less than on interchange, here 16 is greater than 4, unchanged

4 4 7 12 6 24

i++, 12 vs. 7, unchanged, 12 and 4 compared, unchanged

4 4 7 6 24

i++,12 compared with 16, less than, then becomes 4 4 7 <> 12 16 6 24,12 compared with 4, greater than 4, so 12 put 16 in the original position

4 4 7 6 24

Similarly, compare elements of the same color and sort them.

.

.

.

After the second trip: 4 4 6 12 7 16 12 24

.

.

.

Until Gap=1

The sort result for each trip is (input:4 6 7 4)

7 4 4 16 12 12 6 24

4 4 6 12 7 16 12 24

4 4 6 7 12 12 16 24

4 4 6 7 12 12 16 24

Word:

Gap is the value of HK, the interval between elements, the problem is gap=4,2,1.

The outermost loop is the value of the gap. (Divide each time by two), and then in the loop, start inserting the elements that are separated from the gap until the last trip compares the elements that are adjacent to each other.

The Java sort algorithm of 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.