[Python-*-algorithm] Hill sorting

Source: Internet
Author: User
Document directory
  • Brief description:
  • Code
Hill sorting

# Reference books and addresses

Solving with algorithms and data structures

The original Article provides online debugging functions, which are easy to use.

Brief description:

Hill sorting, also known as "minimum incremental sorting", improves efficiency by breaking the original sequence into several subsequences. Each small sequence uses insert sorting. How to divide these subsequences is the key to Hill sorting. Instead of directly splitting the entire sequence into consecutive subsequences, Hill sorting uses an incremental I, sometimes called Gap (interval), to form a subsequence by selecting the list divided by I.

See Figure 6. The entire list has nine elements. If we use 3 as the increment, there will be three sub-lists, each of which can be sorted by insertion. After all the sub-lists are sorted, we can see Figure 7. Although the results are not fully sorted, some interesting things happen. By sorting sub-sequences, we place these elements very close to the final sorting results.

Figure 6


Figure 7

Figure 8 shows the use of the most incremental insert sorting, that is, the standard insert sorting. Note that the entire sorting has been reduced by sorting the sublist.
Total number of operations. The entire process can be completed with a maximum of four shifts.


Figure 8


Figure 9

As we have mentioned above, how to select the increment of sorting and segmentation is a unique feature of hill sorting. In the sample code, we use different increments. This time we started to use n/2 subsequences. Then, N/4 subsequences. Finally, the entire list is sorted by a basic insert order. Figure 9 shows an example of using this increment.

The following shellsort function shows the partial sorting after each increment. The sorting starts from one increment before the last insertion sorting.

Code
# Utf8.py # python2.7 sellsort. pydef shellsort (alist): sublistcount = Len (alist) // 2 # Calculate the increment of the sublist. The first time is 4 ex: alist [0] alist [4] alist [8] is a group of while sublistcount> 0: # The second sublistcount = 2 ex: alist [0] alist [2] alist [4] alist [6] alist [8] is a sub-group for startposition in range (sublistcount): gapinsertionsort (alist, startposition, sublistcount) print ("after increments of size", sublistcount, "the list is", alist) sublistcount = sublistcount // 2def gapinsertionsort (alist, start, GAP ): '''sort the inserts into the subgroups ''' for I in range (start + gap, Len (alist), GAP ): currentvalue = alist [I] position = I while position> = gap and alist [position-Gap]> currentvalue: alist [position] = alist [position-Gap] position = position-gap alist [position] = currentvaluealist = [54,26, 93,17, 77,31, 44,55, 20] # shellsort (alist) # Call sort print (alist)

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.