环境:python3.6、Anaconda 5.1、Jupyter notebook、numba。
我使用Python生成的随机数组来测量shell排序的时间复杂度,但发现它的时间复杂度更符合NlogN。我知道shell排序的时间复杂度是O(n^2),我很困惑。
外壳排序代码:
def shell_sort(list):
n = len(list)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = list[i]
j = i
while j >= gap and list[j - gap] > temp:
list[j] = list[j - gap]
j -= gap
list[j] = temp
gap = gap // 2
return list