def fn():
def part(lo,hi):
j = lo - 1
piv = arr[hi]
for i in range(lo,hi+1):
if arr[i]<=piv:
j+=1
arr[j],arr[i] = arr[i],arr[j]
return j
def sel(k,lo,hi):
piv = part(lo,hi)
if piv == k-1:
return arr[piv]
elif piv < k - 1:
return sel(k,piv+1,hi)
else:
return sel(k,lo,piv-1)
arr = [17,20,4,7,101,11,201]
print(arr)
k = 7
ans = sel(k,0,len(arr)-1)
print("On position %d is %d"%(k,ans))
arr.sort()
print(arr)
fn()
ZGVmIGZuKCk6CiAgICBkZWYgcGFydChsbyxoaSk6CiAgICAgICAgaiA9IGxvIC0gMQogICAgICAgIHBpdiA9IGFycltoaV0KICAgICAgICBmb3IgaSBpbiByYW5nZShsbyxoaSsxKToKICAgICAgICAgICAgaWYgYXJyW2ldPD1waXY6CiAgICAgICAgICAgICAgICBqKz0xCiAgICAgICAgICAgICAgICBhcnJbal0sYXJyW2ldID0gYXJyW2ldLGFycltqXQogICAgICAgIHJldHVybiBqCiAgICBkZWYgc2VsKGssbG8saGkpOgogICAgICAgIHBpdiA9IHBhcnQobG8saGkpCiAgICAgICAgaWYgcGl2ID09IGstMToKICAgICAgICAgICAgcmV0dXJuIGFycltwaXZdCiAgICAgICAgZWxpZiBwaXYgPCBrIC0gMToKICAgICAgICAgICAgcmV0dXJuIHNlbChrLHBpdisxLGhpKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHJldHVybiBzZWwoayxsbyxwaXYtMSkKICAgIGFyciA9IFsxNywyMCw0LDcsMTAxLDExLDIwMV0KICAgIHByaW50KGFycikKICAgIGsgPSA3CiAgICBhbnMgPSBzZWwoaywwLGxlbihhcnIpLTEpCiAgICBwcmludCgiT24gcG9zaXRpb24gJWQgaXMgJWQiJShrLGFucykpCiAgICBhcnIuc29ydCgpCiAgICBwcmludChhcnIpCmZuKCkK
[17, 20, 4, 7, 101, 11, 201]
On position 7 is 201
[4, 7, 11, 17, 20, 101, 201]