算法:快速排序
1 # coding:utf-8 2 3 import random 4 import time 5 6 t = time.time() 7 8 # 快速排序的分割算法, 参考自编程珠玑 9 # [l, r]为闭区间 10 def partion(a, l, r): 11 # tmp = a[0] 12 if l >= r: 13 return 14 m = l 15 #for i in range(1, len(a)): 16 for i in range(l + 1, r + 1): 17 if a[i] < a[l]: 18 m += 1 19 a[m], a[i] = a[i], a[m] 20 21 a[l], a[m] = a[m], a[l] 22 partion(a, l, m - 1) 23 partion(a, m + 1, r) 24 return m 25 26 #def qsort(): 27 if __name__ == "__main__": 28 N = 100000 29 a = [random.randint(1,100000) for i in range(N)] 30 partion(a, 0, N - 1) 31 print a 32 b = a[:]; a.sort() 33 print b == a 34 print time.time() - t
快速排序的Python实现
参考文献:编程珠玑