码迷,mamicode.com
首页 > 编程语言 > 详细

Algorithm: quick sort implemented in python 算法导论 快速排序

时间:2015-02-26 14:51:41      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

 

 1 import random
 2 
 3 def partition(A, lo, hi):
 4     pivot_index = random.randint(lo, hi)
 5     pivot = A[pivot_index]
 6     A[pivot_index], A[hi] = A[hi], A[pivot_index]
 7     store_index = lo
 8     for i in range(lo, hi):
 9         if A[i] < pivot:
10             A[i], A[store_index] = A[store_index], A[i]
11             store_index = store_index + 1
12     A[store_index], A[hi] = A[hi], A[store_index]
13     return store_index
14 
15 def quicksort(A, lo, hi):
16     if lo < hi:
17         p = partition(A, lo, hi)
18         quicksort(A, lo, p - 1)
19         quicksort(A, p + 1, hi)
20 
21 if __name__ == __main__:
22     l = [random.randint(1, 113) for i in range(18)]
23     print l
24     quicksort(l, 0, len(l) - 1)
25     print l

 

Algorithm: quick sort implemented in python 算法导论 快速排序

标签:

原文地址:http://www.cnblogs.com/ydlme/p/4301202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!