我正在學習使用Python,並開始我正在編寫某種程序。 我嘗試了一百個可能的更改,但我不知道如何解決它。 這裏是我的代碼:試圖在python上實現QuickSort:NameError:未定義全局名稱'x'
from random import *
from time import *
L = []
for m in range(0,100):
L.append(randint(0,100))
def quicksort(L, first, last):
i = first
j = last
pivote = (L[i] + L[j])/2
while i < j:
while L[i] < pivote:
i+=1
while L[j] > pivote:
j-=1
if i <= j:
x == L[j]
L[j] == L[i]
L[i] == x
i+=1
j-=1
if first < j:
L == quicksort(L, first, j)
if last > i:
L == quicksort(L, i, last)
return L
start = time()
print ("Disordered:" , L)
L = quicksort(L, L[0], L[len(L)-1])
print ("Ordered:" ,L)
print ("%.2f seconds" % (time() - start))
,這是錯誤:
Traceback (most recent call last):
File "/Users/Ricardo/Desktop/QuickSortR.py", line 48, in <module>
L = quicksort(L, L[0], L[len(L)-1])
File "/Users/Ricardo/Desktop/QuickSortR.py", line 28, in quicksort
x == L[j]
NameError: global name 'x' is not defined
感謝您幫助我!
非常感謝! – 2013-02-15 22:48:58
@RicardoMoctezumaS您應該接受其中一個答案(左邊的大複選框)。 – tacaswell 2013-02-16 02:21:40