有本书叫做《A byte of Python》,只有短短的一百来页,有经验的程序猿在半天就可以学会python。
def partition(list, beg, end):
pivot = list[end-1]
i = beg - 1
for j in range(beg, end-1):
if list[j] <= pivot:
i = i + 1 #no ++ or -- operator in python
list[i], list[j] = list[j], list[i] #swap lsit[i],list[j]
list[i+1], list[end-1] = list[end-1], list[i+1] #swap list[i+1],list[end-1]
return i + 1
def sort(list, beg, end):
if beg < end - 1:
q = partition(list, beg, end)
sort(list, beg, q)
sort(list, q+1, end)
list = [1,5,1,14,6,1,6,8]
sort(list, 0, len(list))
print list
python中交换两个变量不需要另外设置中间变量,
a, b = b, a
就可以交换a和b的值,而且效率更高