Using Python to implement 8 sorting algorithms-Hill sort

Source: Internet
Author: User
The basic idea of hill sort:

The hill sort is an improvement based on the insertion sort, since the insertion sort is efficient for the sorted array operation, but the insertion sort is generally inefficient because only one bit can be moved at a time. So the hill sort is sorted first by grouping until the grouping increment is 1.

Cases:

arr = [49,38,04,97,76,13,27,49,55,65], group increment is 5 o'clock, red number is a group, insert sort, loop traversal sequentially

arr = [13,38,04,97,76,49,27,49,55,65], after the traversal is complete, the grouping increment is reduced,

arr = [13,27,04,55,65,49,38,49,97,76], and then continues the insertion sort for groups with group increments of 2 until the grouping increment is 1

Code:

Python code

def shell_sort (lists):

#希尔排序

Count = Len (lists)

Step = 2

Group = Count/step

While group > 0: #通过group增量分组循环

For I in range (0, group):

j = i + Group

While J < count: #分组中key值的索引, with incremental self-increment

K = J-group

Key = Lists[j]

While K >= 0: #分组中进行插入排序

If LISTS[K] > key:

Lists[k + Group], lists[k] = lists[k], key

Else:break

K-= Group

J + = Group

Group/= Step

return lists

  • 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.