借用快排的思想,每次选取一个参考值,把区间分成两块,左边都小于参考值,右面都大于参考值(中间如果有剩下就是和参考值相同),然后根据k再去选择排左面还是排右面
def select(a,l,r,k):
i = l
j = r
print 'bg',i,j,k
key = a[(l+r)>>1]
while i<=j:
while a[i]key: j -= 1
if (i <= j):
temp = a[i]
a[i]= a[j]
a[j] = temp
i += 1
j -= 1
print i,j,k
print a
if (k <= j and j > l): select(a,l,j,k)
elif k >= i and i < r: select(a,i,r,k)
a = [12,9,7,6,3,2,0,1,4,5,10,11,8]
k = 7
select(a,0,len(a)-1,k)
print a