3
\$\begingroup\$

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
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 8, 2015 at 14:11
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Could you please describe shortly, what the algorithm is supposed to do, or provide a link to a description (e.g. Wikipedia)? \$\endgroup\$ Commented Feb 8, 2015 at 14:33
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Feb 8, 2015 at 15:18

1 Answer 1

1
\$\begingroup\$

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)
answered Feb 10, 2015 at 15:49
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 25, 2015 at 18:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.