Hill sort Shell Sort

Source: Internet
Author: User

The Hill sort (Shell sort) is a kind of insertion sort, which is an improvement on the direct insertion sorting algorithm. This method is also known as narrowing the incremental sort, because of the DL. The shell was named after it was introduced in 1959.

The hill sort is essentially a grouping insertion method. Its basic idea is: for N to sort the series, take an integer less than n Gap (gap is called step) to sort the elements into several groups of sub-sequences, all the distance is a multiple of the gap is placed in the same group; then, the elements within each group are directly inserted into the sort. Once this sequence is complete, the elements of each group are ordered. The gap value is then reduced, and the grouping and sorting are performed repeatedly. Repeat this operation, when the gap=1, the whole sequence is orderly.

Section 1 Tour: (gap=4)

When gap=4, it means dividing the number of columns into 4 groups: {80,20},{30,10},{60,50},{40,70}. Corresponding sequence: {80,30,60,40,20,10,50,70}
Sort the 4 groups, sorting the results: {20,80},{10,30},{50,60},{40,70}. Corresponding sequence: {20,10,50,40,80,30,60,70}

Tour No. 2:(gap=2)

When gap=2, it means dividing the number of columns into 2 groups: {20,50,80,60}, {10,40,30,70}. Corresponding sequence: {20,10,50,40,80,30,60,70}
Note : {20,50,80,60} There are actually two sequential sequences. {20,80} and the {50,60} composition.
{10,40,30,70} There are actually two sequential sequences. {10,30} and the {40,70} composition .
Sort the 2 groups, sorting the results: {20,50,60,80}, {10,30,40,70}. Corresponding sequence: {20,10,50,30,60,40,80,70}

Section 3 Tour: (gap=1)

Hill Sort Time Complexity
The time complexity of hill sorting is related to the selection of increments (i.e., step Gap). For example, when the increment is 1 o'clock, the hill sort degenerates into a direct insert sort, where the time complexity is O (N²), while the Hibbard increment of the hill sort has a time complexity of O (N3/2).

/*** Reference:http://www.cnblogs.com/skywang12345/p/3597597.html*/ Public classShellsort {/*** Hill Sort * * Parameter description: A--the array to be sorted N--the length of the array*/     Public Static voidShellSort1 (int[] A,intN) {//gap is the step size, each reduced to the original half.          for(intGap = N/2; Gap > 0; Gap/= 2) {            //A total of gap groups, performing a direct insert sort for each group             for(inti = 0; I < gap; i++) {                 for(intj = i + gap; J < N; J + =Gap) {                    //if A[J] < A[j-gap], look for the a[j] position and move the position of the trailing data back.                     if(A[j] < a[j-Gap]) {                        intTMP =A[j]; intK = J-Gap;  while(k >= 0 && A[k] >tmp) {A[k+ Gap] =A[k]; K-=Gap; } a[k+ Gap] =tmp; }                }            }        }    }    /*** Sort a single group in the hill sort * * Parameter description: A--the array to be sorted N--the total length of the array I--the starting position of the group gap--the step of the group * Group is "Starting from I, will be separated from the gap length of the number     All taken out "of the composition! */     Public Static voidGroupsort (int[] A,intNintIintGap) {         for(intj = i + gap; J < N; J + =Gap) {            //if A[J] < A[j-gap], look for the a[j] position and move the position of the trailing data back.             if(A[j] < a[j-Gap]) {                intTMP =A[j]; intK = J-Gap;  while(k >= 0 && A[k] >tmp) {A[k+ Gap] =A[k]; K-=Gap; } a[k+ Gap] =tmp; }        }    }    /*** Hill Sort * * Parameter description: A--the array to be sorted N--the length of the array*/     Public Static voidShellSort2 (int[] A,intN) {//gap is the step size, each reduced to the original half.          for(intGap = N/2; Gap > 0; Gap/= 2) {            //A total of gap groups, performing a direct insert sort for each group             for(inti = 0; I < gap; i++) Groupsort (A, n, I, GAP); }    }     Public Static voidMain (string[] args) {inti; intA[] = {80, 30, 60, 40, 20, 10, 50, 70,90,110,120 }; System.out.printf ("Before sort:");  for(i = 0; i < a.length; i++) System.out.printf ("%d", A[i]); System.out.printf ("\ n"); //ShellSort1 (A, a.length);ShellSort2 (A, a.length); System.out.printf ("After sort:");  for(i = 0; i < a.length; i++) System.out.printf ("%d", A[i]); System.out.printf ("\ n"); }}

Hill sort Shell Sort

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.