0
這是我輸入:Python的快速排序沒有排序右側
[3, 9, 8, 4, 6, 10, 2, 5, 7, 1]
這裏是我的代碼(這還沒有實現的另一個原因名爲count):
def Count(num_list):
global comparisons
if len(num_list) > 1:
pivot = num_list[0]
print "Pivot value is: %s, current list is: %s" % (pivot, num_list)
i = 1
j = 1
for j in range(len(num_list)):
if num_list[j] < pivot:
print "Swapping %s and %s" % (num_list[i], num_list[j])
num_list[i], num_list[j] = num_list[j], num_list[i]
print "List is now: %s" % (num_list)
i += 1
num_list[0], num_list[i-1] = num_list[i-1], num_list[0]
print "List before next recursive step: %s" % num_list
Count(num_list[:(len(num_list)/2)])
Count(num_list[(len(num_list)/2):])
print "List at the end of function: %s" % (num_list)
我得到以下結果如下:
[1, 2, 3, 4, 6, 10, 9, 5, 7, 8]
當我看到所有我所做的調試打印語句時,我發現它並沒有將右半部分放在一起它左側的方式,但我無法弄清楚。
http://codexpi.com/quicksort-python-iterative-recursive-implementations/在谷歌上30秒。 – Pythonista
@Pythonista這不是他的問題,因爲這將脫離主題。他的問題是關於他的具體實施。 – Natecat
您的代碼只會將列表分成兩部分,一部分高於主鍵,另一半低於主鍵。它不排序列表。 – Natecat