Insert sort--Hill sort

Source: Internet
Author: User

Hill sort

Hill Sort is also a kind of insertion sort. Also known as narrowing the incremental sort. It is an improvement to the direct insertion sort.

The direct insert sort is compared with the previous element each time. Its step size is 1. Hill sort is otherwise, it has the step size is dynamic change, or from large to small changes. The hill sort divides the entire sequence into n groups according to the step size. The ordering of elements is then performed within each subgroup. Then reduce the step size, the corresponding sequence is divided into sub-groups will be reduced, the number of elements within each grouping is correspondingly increased. The elements inside the sub-group are then sorted. Narrow the step again, sort again .... Until the step is 1, that is, the entire sequence has only one subgroup, which is the sequence itself. This is the time to sort the final sort.

Suppose there is such a sequence of strings

A=[23,19,31,47,52,16,45,21]
  

: (from small to large sort)

The first set step is 4, and the same color is a grouping. Then each element within the same color box is compared to each other, resizing, because it is from small to large, so small in front, big in the back. To get sorted results.

Then adjust the step and group again

:

The second step adjustment is divided into two groups for the original half. Repeat the action of sorting the elements in the same color box, small in front, big in the back, get sort results

Resize step again, group again

The third step adjustment, Step 1, group 1, this is the largest group. Or the actions that are ordered within the repeating group. Get the final result.

Hill sort is like this, grouping--sorting--grouping--sorting--grouping--the sort finally gets the final result.

The code is as follows:

defShellsort (A): N=Len (A) Gap=n//2#sets the initial step and also represents the number of groupings.      whileGap>0:#The step is the minimum number of groups is 1, as long as not 1 repeats the following grouping--sort action         forIinchRange (Gap,n):#Why start with gap? I'll tell you.Temp=a[i]#Remove the first unordered element within each groupingJ=i-gap#gets the largest ordinal element index within the group .             whileJ>=0 andTEMP<A[J]:#compares the first element of an unordered area to an element of an ordered area within that group. A[J+GAP]=A[J]#because the elements between each group are separated by the gap (the current step) of the other groups of elements, you should also pay attention when assigning values. J-=gap#if the while loop condition has been satisfied, move forward as the current stepA[j+gap]=temp#exiting the while loop means that the appropriate insertion position has been found. Inserts the current element into the appropriate locationGap = Gap//2#In this step, all the groupings are sorted, then adjust the step size, reduce the number of groups, expand the group length. To sort again    returnAa=[23,19,31,47,52,16,45,21]Print(Shellsort (a))
View Code

Why do I have to start with gap when I loop?

The hill sort is an improved version of the direct insertion sort. The direct insert sort step is 1, and the direct insert sort grouping also has only one large grouping, which is itself. and Hill sort because the step is not 1, so its grouping is also multiple. Although it is multiple groupings, each grouping is sorted internally, and is the same as the direct insert sort-starting with the second element within the grouping, and comparing it to the previous element, until it finds a suitable location. Just this time, the previous element index is no longer i-1, but i-gap. Or because of the existence of step size.

In this case, gap= 4

Why start with 4. Because the first three elements, I-gap are less than 0, they have no previous element within their respective groupings, they are themselves the first element of their respective groupings. So just start right from 4. Of course, it can be forced to start from 0, but there is no practical meaning, do not work hard only.

Insert sort--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.