快速排序的python实现。
# -*- coding:utf-8 -*-
'''
qsort
'''
def qsort_partition(A,p,r):
x = A[r]
i = p - 1
for j in range(p,r):
if A[j] < x:
i += 1
A[j],A[i] = A[i],A[j]
i += 1
A[r],A[i] = A[i],A[r]
return i
def qsort_routine(A,p,r):
if p < r:
q = qsort_partition(A,p,r)
qsort_routine(A,p,q-1)
qsort_routine(A,q+1,r)
return
def qsort(A):
qsort_routine(A,0,len(A)-1)
return
if __name__ == '__main__':
A = [1,6,3,8,5,9,0,7,2,4]
print A
qsort(A)
print A