Hill sort and Hill sort Java code

Source: Internet
Author: User

Original link: http://www.orlion.ga/193/

You can see the hill sort by agreeing to an interval (4 in the figure), then inserting the data for 0, 4, 8, three, and then moving right one to the position 1, 5, 9 for insertion sort by this rule until all the sort is involved. The interval is then contracted to 4-1=3, and then the sorting method is continued as above. The process is as follows:

9 1 2 3 0 4 5 7 6 8

SETP 1 after the interval of 4 sorted into: 0 1 2 3 6 4 5 7 9 8

SETP 2 After the interval of 3 sorted into: 0 1 2 3 6 4 5 7 9 8

SETP 3 after the interval of 2 sorted into: 0 1 2 3 5 4 6 7 9 8

SETP 4 after the interval of 1 sorted into: 0 1 2 3 4 5 6 7 9 8

Reduce the interval: for large arrays should be larger at the beginning of the interval, and the reduction of the interval should also be larger, such as for a 1000-size array to be sorted by the interval: 364, 121, 40, 13, 4, 1, here the interval sequence is presented by Knuth, The sequence starts in reverse form 1 and is generated by the recursive expression h=3*h+1, with an initial value of 1. (Other methods can also produce sequences) as shown in the following table

Here's The hill sort Java code

Public static void shellsort (Long[] arr , int elenum) {     int i,j;    long temp;    //  first Find out h     int h = 1;    while (h < elenum / 3) H  = 3 * h + 1; // 1, 4    while  (h >  0)  {for (i = h ; i < elenum ; i = i + &NBSP;H)  {    //  sort pairs of arrays     temp = arr[i];     j = i;    while (j > h-1 && temp &NBSP;&LT;=&NBSP;ARR[J-H])  {        arr[j] = arr[j-h]; j-=h;    }        arr[j] = temp;         }h =  (h-1)/3;    }    system.out.println ( Arrays.tostring (arr));}

Hill sort and Hill sort Java code

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.