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 quicksort(lo,hi):
piv = part(lo,hi)
if lo < piv - 1:
quicksort(lo, piv - 1)
if piv + 1 < hi:
quicksort(piv + 1, hi)
def select(k,lo,hi):
piv = part(lo,hi)
if piv == k-1:
return arr[piv]
elif k-1>piv:
return select(k,piv+1,hi)
return select(k,lo,piv-1)
arr = [14,4,5,21,3,33,23]
n = len(arr)
print(arr)
for i in range(n):
print("%d => %d"%(i+1,select(i+1,0,n-1)))
quicksort(0, n-1)
print(arr)
fn()
ZGVmIGZuKCk6CiAgICBkZWYgcGFydChsbyxoaSk6CiAgICAgICAgaiA9IGxvIC0gMQogICAgICAgIHBpdiA9IGFycltoaV0KICAgICAgICBmb3IgaSBpbiByYW5nZShsbywgaGkrMSk6CiAgICAgICAgICAgIGlmIGFycltpXTw9cGl2OgogICAgICAgICAgICAgICAgais9MQogICAgICAgICAgICAgICAgYXJyW2pdLGFycltpXT1hcnJbaV0sYXJyW2pdCiAgICAgICAgcmV0dXJuIGoKCiAgICBkZWYgcXVpY2tzb3J0KGxvLGhpKToKICAgICAgICBwaXYgPSBwYXJ0KGxvLGhpKQogICAgICAgIGlmIGxvIDwgcGl2IC0gMToKICAgICAgICAgICAgcXVpY2tzb3J0KGxvLCBwaXYgLSAxKQogICAgICAgIGlmIHBpdiArIDEgPCBoaToKICAgICAgICAgICAgcXVpY2tzb3J0KHBpdiArIDEsIGhpKQoKICAgIGRlZiBzZWxlY3QoayxsbyxoaSk6CiAgICAgICAgcGl2ID0gcGFydChsbyxoaSkKICAgICAgICBpZiBwaXYgPT0gay0xOgogICAgICAgICAgICByZXR1cm4gYXJyW3Bpdl0KICAgICAgICBlbGlmIGstMT5waXY6CiAgICAgICAgICAgIHJldHVybiBzZWxlY3QoayxwaXYrMSxoaSkKICAgICAgICByZXR1cm4gc2VsZWN0KGssbG8scGl2LTEpCgogICAgYXJyID0gWzE0LDQsNSwyMSwzLDMzLDIzXQogICAgbiA9IGxlbihhcnIpCiAgICBwcmludChhcnIpCiAgICBmb3IgaSBpbiByYW5nZShuKToKICAgICAgICBwcmludCgiJWQgPT4gJWQiJShpKzEsc2VsZWN0KGkrMSwwLG4tMSkpKQogICAgcXVpY2tzb3J0KDAsIG4tMSkKICAgIHByaW50KGFycikKZm4oKQo=
[14, 4, 5, 21, 3, 33, 23]
1 => 3
2 => 4
3 => 5
4 => 14
5 => 21
6 => 23
7 => 33
[3, 4, 5, 14, 21, 23, 33]