基本的快排算法,二分法
1 #!/usr/bin/env python 2 # encoding: utf-8 3 4 l1=[1,4,2,6,3] 5 6 def path_sort(l,start_index,end_index): 7 flag = l[end_index] 8 i = start_index - 1 9 for j in range(start_index,end_index): 10 if l[j] > flag: 11 pass 12 else: 13 i += 1 14 tmp = l[i] 15 l[i] = l[j] 16 l[j] = tmp 17 tmp = l[end_index] 18 l[end_index] = l[i+1] 19 l[i+1] = tmp 20 21 return i+1 22 23 def quick_sort(l,start_index,end_index): 24 if start_index >= end_index: 25 return 26 middle = path_sort(l,start_index,end_index) 27 quick_sort(l,start_index,middle-1) 28 quick_sort(l,middle+1,end_index) 29 print l,len(l) 30 31 quick_sort(l1,0,len(l1)-1)