Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

Problem

Algorithm

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
share|improve this question
1  
Could you please describe shortly, what the algorithm is supposed to do, or provide a link to a description (e.g. Wikipedia)? – Attilio Feb 8 '15 at 14:33
    
this is the basic problem en.wikipedia.org/wiki/Selection_algorithm and this is the algorithm that i'm implementing.. en.wikipedia.org/wiki/Quickselect and sorry for not including them before – mr.eightnoteight Feb 8 '15 at 15:18

You should learn about how to benchmark your code, from my benchmark, shuffling makes the algorithm slower:

from random import shuffle,randint
import time

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,shuffling=None):
    assert lo <= spos < hi
    if shuffling: 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

def random_list(length):
    return [randint(1,100) for _ in range(length)]

start = time.time()
results = []
for _ in range(10000):
    results.append(select(random_list(10),1,5,1,shuffling=True))
print("With shuffling: ",time.time()-start)

start = time.time()
results = []
for _ in range(10000):
    results.append(select(random_list(10),1,5,1,shuffling=False))
print("WithOUT shuffling: ",time.time()-start)
share|improve this answer
    
sorry for my late comment but my intentions are to implement a randomized selection algorithm not the deterministic one. and this is clearly opposite of it. – mr.eightnoteight Feb 25 '15 at 18:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.