fork download
  1. def fn():
  2. def part(lo,hi):
  3. j = lo - 1
  4. piv = arr[hi]
  5. for i in range(lo,hi+1):
  6. if arr[i]<=piv:
  7. j+=1
  8. arr[j],arr[i] = arr[i],arr[j]
  9. return j
  10. def sel(k,lo,hi):
  11. piv = part(lo,hi)
  12. if piv == k-1:
  13. return arr[piv]
  14. elif piv < k - 1:
  15. return sel(k,piv+1,hi)
  16. else:
  17. return sel(k,lo,piv-1)
  18. arr = [17,20,4,7,101,11,201]
  19. print(arr)
  20. k = 7
  21. ans = sel(k,0,len(arr)-1)
  22. print("On position %d is %d"%(k,ans))
  23. arr.sort()
  24. print(arr)
  25. fn()
  26.  
Success #stdin #stdout 0.03s 9512KB
stdin
Standard input is empty
stdout
[17, 20, 4, 7, 101, 11, 201]
On position 7 is 201
[4, 7, 11, 17, 20, 101, 201]