Hill Sort (Shell)

Source: Internet
Author: User

The essence of the hill sort is the grouping insert sort, which is also known as narrowing the incremental sort.

The basic idea of this method is that the whole sequence of elements is divided into several sub-sequences (consisting of elements separated by an "increment"), then the direct insertion sort is done separately, then the increment is reduced and then sorted, and then the whole element is sorted by a direct insertion when the elements in the entire sequence are basically ordered (the increment is small enough). Because the direct insert sort is very efficient when the elements are basically ordered (close to the best case), the hill sort is more efficient in time than the first two methods.

is to directly insert a sort of upgraded version, can be compared with the direct insertion sort, it is easier to understand.

1. Fixed increment r, each time except 2

/* Shell sort * Hill sort direct insert sorted upgrade */static void Shellsort (int[] a) {int j,i,h;int r,temp;int x=0;for (r= a.length/2;r>=1;r/=2 {for (I =r; i<a.length;i++) {temp = A[i];j = I-r;while (j>=0 && temp<a[j]) {a[j+r]=a[j];j = R;} A[j+r]=temp;} x + +; System.out.println ("+x+" step to sort Result: "); for (h = 0;h<a.length;h++) {System.out.print (" "+a[h]+");} System.out.println ("\ n");}}

  2. Custom incremental array: int[] dlta= new int[]{3,2,1}, this array is customized and the number depends on the actual situation.

static void Shellinsert (int[] A,int dk) {int i,j;for (i = dk+1;i<a.length;i++) {if (A[i]<a[i-dk]) {  //need to insert A[0]=a [I];for (j = i-dk;j>0&&a[0]<a[j];j-=dk) a[j+dk]=a[j];     Log back to find the insertion position a[j+dk]=a[0];}} static void Shellsort (int a[],int dlta[],int t) {//by increment sequence dlta[0: T-1] To order table L for Hill sort for  (int k=0;k<t;k++) Shellinsert (A,dlta[k]);}     public static void Main (string[] args) {   int[] array  = new int[]{22,55,45,18,40,8,36};int[] Dlta= new int[]{3,2,1 };//shellsort (array); Shellsort (array,dlta,3);    for (int i=0;i<array.length;i++) System.out.print (array[i]+ "");  }

  

Hill Sort (Shell)

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.