比对结果
随机数组长度为
10000
时:Time for heer is 0.4159576892852783
Time for quick is 0.07650184631347656
随机数组长度为
100000
时:Time for heer is 4.3795859813690186
Time for quick is 0.89105224609375
Sedgewick增量序列的希尔排序
def heerSort(ar):
Sedgewick = [1, 5, 19, 41, 109, 209, 505, 929, 2161,
3905, 8929, 16001, 36289, 64769, 146305,
260609, 587521, 1045505, 2354689, 4188161,
9427969, 16764929, 37730305, 67884289,
150958081, 268386305, 603906049, 1073643521]
i = 0
keeper = 0
while i < 28 :
if Sedgewick[i] > len(ar):
keeper = i - 1
break
i += 1
while keeper >= 0:
i = Sedgewick[keeper]
j = i
while j < len(ar):
p = j
temp = ar[p]
while p > 0 and ar[p - i] > temp:
ar[p] = ar[p - i]
p -= i
ar[p] = temp
j += 1
keeper -= 1
快速排序
如果运行报错,请尝试加入
import sys sys.setrecursionlimit(9000000)
def quickSort(ar, left, right):
if left >= right:
return
low = left
high = right
base = ar[low]
while left < right:
while left < right and ar[right] >= base:
right -= 1
ar[left] = ar[right]
while left < right and ar[left] <= base:
left += 1
ar[right] = ar[left]
ar[right] = base
quickSort(ar, low, left - 1)
quickSort(ar, left + 1, high)