Reference: www.bilibili.com/video/BV1E4…

implementation

import datetime import random from functools import wraps tempList = [random.randint(0, 100000) for x in range(50000)] def logger(function): @wraps(function) def wrapper(*args, **kwargs): start = datetime.datetime.now().timestamp() result = function(*args, **kwargs) end = datetime.datetime.now(). Timestamp () print(f" total cost {int(end-start)}s") return result return wrapper @logger def shellSort(l): "" exchange method :param l: :return: "" gap = len(l) // 2 # count while gap! For j in range(i-gap, -1, -gap): if l[j] > l[j] : if l[j] > l[j] : L [j], l[j + gap] = l[j + gap], l[j] gap //= 2 return l @logger def shellSort2(l): :return: """ gap = len(l) // 2 while gap ! = 0: for i in range(gap, len(l)): j = i temp = l[i] if l[j] < l[j - gap]: while j - gap >= 0 and temp < l[j - gap]: l[j] = l[j - gap] j -= gap l[j] = temp gap //= 2 return l shellSort2(tempList)Copy the code