Sort algorithm--Hill sort

Source: Internet
Author: User

The essence of the hill sort is the grouping insert sort, which is also known as narrowing the incremental sort, because of the DL. The shell was introduced in 1959 and named

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.

  

1 voidHellsort (intUnsort[],Const intcount)2 {3      for(intGap = count/2; Gap >0; Gap/=2)4     {5          for(inti =0; I < gap;i++)6         {7              for(intj = I+gap; J < Count; J + =Gap)8             {9                 inttemp =Unsort[j];Ten                 intK = J-Gap; One                  while(k >0&&unsort[k]>temp) A                 { -Unsort[k + gap] =Unsort[k]; -K-=Gap; the                 } -UNSORT[K+GAP] =temp; -             } -         } +          -     } +}

Test

1 intMain ()2 {3     intUnsort[] = {2,5,7,4,6,9};4Hellsort (Unsort,6);5      for(inti =0; I <6; i++)6     {7printf"%d\r", Unsort[i]);8     }9     return 0;Ten}

Note: For how to calculate the step size, you still need to consult the data.

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.