希尔排序--python
import random import time # 插入排序 def insertion_sort(arr, step): for i in range(step, len(arr)): for j in range(i, step - 1, -step): if arr[j] < arr[j-step]: arr[j], arr[j-step] = arr[j-step], arr[j] else: break # 希尔排序:插入排序算法的优化算法 def shell_sort(arr): length = len(arr) step = 1 while step < length // 3: step = 3 * step + 1 while step >= 1: insertion_sort(arr, step) step = step // 3 if __name__ == ‘__main__‘: arr = [random.randint(0, 1000000) for _ in range(5000)] print(‘待排序数组大小:‘, len(arr)) start = time.time() shell_sort(arr) end = time.time() print(‘用时:‘, end - start)
相关推荐
赶路人儿 2020-11-02
scuyxi 2020-10-25
csdnfelix 2020-10-18
代码之神 2020-10-15
Pokemogo 2020-09-17
MasterCui 2020-09-08
Morelia 2020-09-04
meylovezn 2020-08-30
Hannah 2020-08-19
hugebawu 2020-08-17
scuyxi 2020-08-16
hang0 2020-08-16
higher0 2020-08-11
Tips 2020-08-08
Tristahong 2020-08-05
xiesheng 2020-08-02
huakai 2020-07-26
RememberMePlease 2020-06-26
zangdaiyang 2020-06-25