Sort algorithm---Hill sort

Source: Internet
Author: User

After introducing the previous three basic algorithms, talk about another slightly more advanced algorithm today--Hill sort

Hill sort is according to its design of the name of the hill (Donald Shell) command, it based on the insertion algorithm, the insertion algorithm has been modified, then the insertion algorithm has what characteristics, we recall:

1. The insertion sort is very efficient on the sequenced sequence, time complexity O (n), but in the worst case the time complexity is O (n2)

2. The insertion sort is very inefficient when you move, because you can only move one

Basic idea:

First select an integer less than n d1, the array of the values of all the distance is D1 into a group, successively in each group to insert the sort, and then select the second integer d2<d1 as the second increment, in the whole sequence distance of D2 integer multiples into a group, and then in the group to insert Sort, know the increment is 1, So the whole data will be sorted in order;

The gap that Hill sort algorithm uses is also D1, D2 ... For D1 = LENGHT/2 D2=D1/2, it is best to describe other more efficient packet thresholds

Hill Sort Chart:

1. In the figure we can see that the original array of unsorted length is 10, the first iteration we choose Gap as length/2=5,

2. We divide the entire array into 5 groups, which we can clearly see (8,3) as a group (9,5), and so on.

3. We are inserting a sort within each group, then the result of the sort is shown in the third figure, after which we continue to group, gap = 5/2 = 2, indicating that the entire sequence is divided into two groups

4. Continue to use the direct insert sort, the two sets of data directly into the sorting, in order to perform grouping, sorting, then grouping, reordering, know gap=1, the entire array sorting complete

Then implement the code:

/*** Hill Sort * Divide the array into n groups, n groups using insert sort, after M times, Number ordered * Hill sort of the sequence of the order of the order performance is important, we use the N/2 this amount, its final time complexity or O (n2) * Can use other increments such as Hibbard, The time complexity can be reduced to O (N3/2), Sedgewick time complexity O (N4/3)*/ Public classShellsort { Public Static voidShellsort (int[] arr) {        LongStart =System.currenttimemillis (); //using LENGTH/2 's Gap value, know gap=1         for(intGap = ARR.LENGTH/2; Gap > 0; Gap/= 2) {             for(inti = gap; i < arr.length; i++) {                intj =i; //Use the direct insert sort to sequentially insert each element in the sequence and the element in the same group                 while(j-gap >= 0 && arr[j] < arr[j-Gap]) {                    intTMP =Arr[j]; ARR[J]= Arr[j-Gap]; Arr[j-gap] =tmp; J-=Gap; }}} System.out.printf ("Direct insertion algorithm \ t run time%dms\n", (System.currenttimemillis ()-start)); }     Public Static voidMain (string[] args) {intData_len = 10000; int[] data =New int[Data_len]; Random Rd=NewRandom ();  for(intindex = 0; Index < Data_len; index++) {Data[index]= Rd.nextint (10000); } System.out.println ("Build Data Complete");        Shellsort (data);    ShowData (data); }     Public Static voidShowData (int[] data) {         for(intitem:data) {System.out.printf ("%d,", item);    } System.out.println (); }}

In the code I made a simple comment, starting with the gap to traverse all the data elements, and let the element and the same group of elements directly into the sort, know gap=1

From the intuitive sense what determines the performance of hill sort, that is we choose the gap algorithm, code we use the gap = LENGTH/2 The initial value, the algorithm is not optimal, but well understood, also hill sort recommended, but we may pursue extreme, Then you can check the other gap generation algorithm online, the most used should be

The Hibbard mentioned in the note

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.