Instead of picking a random pivot I'm shuffling the array beforehand. Does it count as randomization and is it efficient to shuffle or pick a random pivot and is my implementation pythonic?
from random import shuffle
def _partition(arr, lo, hi, pivot):
p = arr[pivot]
arr[hi - 1], arr[pivot] = arr[pivot], arr[hi - 1]
i = lo - 1
for j in xrange(lo, hi):
if arr[j] <= p:
i += 1
arr[i], arr[j] = arr[j], arr[i]
return i
def select(arr, lo, hi, spos):
assert lo <= spos < hi
shuffle(arr) # here's your randomization.
while True:
pos = _partition(arr, lo, hi, lo)
if pos == spos:
return arr[pos]
elif pos < spos:
lo = pos + 1
else:
hi = pos