首页 > 其他 > 详细

快速排序算法

时间:2014-05-17 01:21:02      阅读:438      评论:0      收藏:0      [点我收藏+]

快速排序算法。python实现。

bubuko.com,布布扣
 1 # -*- coding: utf8 -*-
 2 
 3 import random
 4 
 5 def partition(mylist, low, high):
 6     pivotkey = mylist[low]
 7     while low < high:
 8         while low < high and mylist[high] >= pivotkey:
 9             high -= 1
10         mylist[low] = mylist[high]
11         while low < high and mylist[low] <= pivotkey:
12             low += 1
13         mylist[high] = mylist[low]
14     mylist[low] = pivotkey
15     return low
16 
17 def quicksort(mylist, low, high):
18     if low < high:
19         pivotloc = partition(mylist, low, high)
20         quicksort(mylist, low, pivotloc-1)
21         quicksort(mylist, pivotloc+1, high)
22 
23 if __name__ == "__main__":
24     mylist = [random.randint(1, 100) for i in xrange(10)]
25     print "排序前: ", mylist
26     quicksort(mylist, 0, len(mylist)-1)
27     print "排序后: ", mylist
bubuko.com,布布扣

测试结果:

bubuko.com,布布扣
1 排序前:  [23, 67, 14, 63, 56, 98, 12, 84, 6, 29]
2 排序后:  [6, 12, 14, 23, 29, 56, 63, 67, 84, 98]
bubuko.com,布布扣

 

快速排序算法,布布扣,bubuko.com

快速排序算法

原文:http://www.cnblogs.com/fyho2012/p/3724961.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!