0
這是我的快速排序算法。很簡單Python中的Quicksort工具運行無停止
x = 0
def swap(list, a, b):
temp = list[a]
list[a] = list[b]
list[b] = temp
return list
def quicksort2(list, left, right):
if right > left:
global x
x = x + 1
print x , list, left, right
l = left+1
r = right
while l <= r :
while list[l] < list[left]:
l = l + 1
while list[r] > list[left]:
r = r - 1
if l < r:
list = swap(list, l, r)
list = swap(list, left, r)
list = quicksort2(list, left, r-1);
return quicksort2(list, r+1, right);
return list
但是,當我運行測試用例
b = list([1, 2, 2, 3, 4, 5, 6, 12, 6, 32])
quicksort2(b, 0, len(b)-1)
結果是
1 [1, 2, 2, 3, 4, 5, 6, 12, 6, 32] 0 9
2 [1, 2, 2, 3, 4, 5, 6, 12, 6, 32] 1 9
,並在此停... 任何人有任何理由...
您的嘗試是不是很簡單,你可能會想只是看這裏:http://en.literateprograms.org/Quicksort_(Python)你的代碼是很複雜以上 – jamylak 2013-03-22 02:46:28
在附註中,'list [a],list [b] = list [b],list [a]'是做交換的pythonic方式 – karthikr 2013-03-22 03:18:10