# encoding=utf-8 def shell_sort(alist): """希尔排序""" n = len(alist) gap = n // 2 while gap >= 1: for j in range(gap, n): i = j while (i-gap) >= 0: if alist[i] < alist[i-gap]: alist[i], alist[i-gap] = alist[i-gap], alist[i] i -= gap else: break gap //= 2 if __name__ == ‘__main__‘: alist = [22, 1, 4, 553, 3, 212, 77] print(alist) shell_sort(alist) print(alist)
时间: 2024-10-05 04:55:04