一、冒泡排序
ll = [7, 4, 3, 67, 34, 1, 8] def maopao(ll): n = len(ll) for j in range(0, n - 1): for i in range(0, n - 1 - j): if ll[i] > ll[i + 1]: ll[i], ll[i + 1] = ll[i + 1], ll[i]
maopao(ll)
print(ll)
时间复杂度O(n)
希尔排序
def shellSort(arr): n = len(arr) gap = int(n/2) while gap > 0: for i in range(gap,n): temp = arr[i] j = i while j >= gap and arr[j-gap] >temp: arr[j] = arr[j-gap] j -= gap arr[j] = temp gap = int(gap/2) arr = [ 12, 34, 54, 2, 3] n = len(arr) print ("排序前:") for i in range(n): print(arr[i]), shellSort(arr) print (" 排序后:") for i in range(n): print(arr[i])