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