Starting from the sort of Java (2)--Hill sort

Source: Internet
Author: User

The Hill sort (Shell sort) is a sort of insertion. 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.

Basic idea: The algorithm will sort the group of numbers according to an increment D (n/2,n to the number of numbers to be sorted) into several groups, each group of records of the subscript difference D. Direct insert sorting of all elements in each group, followed by a smaller increment (D/2) to group it, followed by a direct insert sort in each group. When the increment is reduced to 1 o'clock, the sort is completed after the direct insert sort.

Code implementation:

1  Packagesort;2 3  Public classShellsort {4      Public Static voidMain (String[]args) {5         int[]array={49,38,65,97,76,13,27,49,55,4};6         DoubleLength=Array.Length;7         8         intTemp=0;9          while(true){//use D to determine the number of outermost loopsTenLength=math.ceil (LENGTH/2); One            intD= (int) (length); A                  for(intx=0; x<d;x++){ -                      for(inti=x+d;i<array.length;i+=d) { -temp=Array[i]; the                         intj=i-D; -                          for(; j>=0&&temp<array[j];j-=d)//Insert Sort -array[j+d]=Array[j]; -array[j+d]=temp; +                      -                     } +                      A                 } at              -             if(d==1) Break; -  -     } -          for(inti=0;i<array.length;i++) -System.out.print (array[i]+ ""); in     } -          to  +}

Starting from the sort of Java (2)--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.