标签:style blog class code c java
快速排序算法。python实现。
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
测试结果:
1 排序前: [23, 67, 14, 63, 56, 98, 12, 84, 6, 29] 2 排序后: [6, 12, 14, 23, 29, 56, 63, 67, 84, 98]
标签:style blog class code c java
原文地址:http://www.cnblogs.com/fyho2012/p/3724961.html