Insert sort-Hill sort (Shell ' s sort) principle and Java implementation

Source: Internet
Author: User

The hill sort was proposed by D.l.shell in 1959, and the relative direct ranking has been greatly improved. Hill sort is also called narrowing the incremental sort

Basic idea:

First, the entire sequence of records to be sorted into a number of sub-sequences of the direct insertion of the order, the entire series of records in the "Basic order", then the whole record is inserted in order.

Operation Method:

    1. Select an incremental sequence T1,T2,...,TK, where ti>tj,tk=1;
    2. According to the number of increment series K, the sequence is sorted by K-trip;
    3. Each order, according to the corresponding increment ti, the backlog sequence is divided into several sub-sequences of length m, respectively, the sub-table is directly inserted sort. Only the increment factor is 1 o'clock, the entire sequence is treated as a table, and the length of the table is the length of the entire sequence.

Example of a hill sort:


Algorithm implementation:

We simply handle the increment sequence: Delta sequence d = {N/2, N/4, N/8 ..... 1} n is the number of digits to be sorted

That is: A set of records to be sorted by an increment D(the number of n/2,n to be sorted) into several sets of sub-sequences, each group of recorded subscript difference D. direct insertion of all elements in each group, It is then grouped with a smaller increment (d/2), followed by a direct insert sort in each group. Continue to shrink the increment until it is 1, and finally use the direct insert sort to complete the sorting.

1 /**2  *3  * @authorZhangtao4  */5  Public classShellinsertsort6 {7      Public Static voidMain (string[] args)8     {9         intarr[]={49,38,65,97,76,13,27,49,55,4};Ten Shellsort (arr); One     } A      -     /**  - * Direct Insert sort in general form the     * @paramint DK Shrink increment, if direct insert sort, dk=1 -     *  -     */   -    Static voidShellinsertsort (intA[],intDK) +    {   -         intn=a.length; +          for(intI= DK; i<n; ++i) {   A            if(A[i] < A[I-DK]) {//if the first element is greater than the i-1 element, it is inserted directly. Less then, move the ordered table after inserting at                intj = IDK;  -                intx = A[i];//Copy as Sentinel, which stores the elements to be sorted -A[i] = A[I-DK];//first move back one element -                 while(x < a[j]) {//find the insertion position in an ordered table -A[J+DK] =A[j];  -J-= DK;//element Move back in                    if(j<0) -                    { to                         Break; +                    } -                }   theA[J+DK] = x;//Insert to correct position *            }   $ PrintLine (A, I); Panax Notoginseng         }   -    }   the  +    /**  A * First by increment D (N/2,n for the number of orders to be sorted by Hill sort the     */   +    Static voidShellsort (inta[]) {   -        intn=a.length; $        intDK = N/2;  $         while(DK >= 1 ){   - Shellinsertsort (A, DK);  -DK = DK/2;  the        }   -    }  Wuyi      the      //Print the sort results each time -     Static voidPrintLine (int[] arr,inti) Wu     { -System.out.println (i+ ":"); About         intArrlength=arr.length; $          for(intj=0;j<arrlength;j++) -         { -System.out.print (arr[j]+ ""); -         } A System.out.println (); +     } the}

It is difficult for hill to sort the time-lapse analysis, the comparison number of key code and the number of record moves depend on the selection of increment factor sequence d, which can accurately estimate the comparison number of key codes and the number of movement of records in certain cases. At present, no one has given a method to select the best increment factor sequence. increment factor sequence can have a variety of ways, there are odd, but also take prime numbers, but need to note: The increment factor in addition to 1 there is no public factor, and the last increment factor must be 1. The hill sort method is an unstable sort method.

Insert sort-Hill sort (Shell ' s sort) principle and Java implementation

Related Article

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.