Shell Sort in Python
Shell sort, by Donald Shell, works by taking a gap that is n/2 elements apart (rounded down), compares each element with an element that is away by a distance of the gap, and then takes half the gap on the next iteration. When the gap is zero, the elements are sorted.
The following is an implementation of Shell sort in Python.
def shell_sort(a_list):
gap = int(len(a_list)/2)
while gap > 0:
for i in range(len(a_list)-gap): # 01234 5/2 0-2,1-3,2-4 # 0123 4/2 0-2,1-3
if i+gap < len(a_list) and a_list[i] > a_list[i+gap]:
a_list[i], a_list[i+gap] = a_list[i+gap], a_list[i]
gap = int(gap/2)
my_list = [6, 3, 5, 4, 1, 1, 2]
shell_sort(my_list)
print(my_list)