码迷,mamicode.com
首页 > 其他好文 > 详细

快速排序算法

时间:2014-05-17 01:25:40      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

快速排序算法。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

快速排序算法

标签:style   blog   class   code   c   java   

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

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