Initial solution to homework
This commit is contained in:
15
qselect.py
Normal file
15
qselect.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import random
|
||||
|
||||
def qselect(i, xs):
|
||||
if xs == []: return None
|
||||
|
||||
pivot = xs.pop(random.randrange(len(xs)))
|
||||
left = [x for x in xs if x < pivot]
|
||||
right = [x for x in xs if x >= pivot]
|
||||
|
||||
if i > len(left):
|
||||
return qselect(i - len(left) - 1, right)
|
||||
elif i == len(left):
|
||||
return pivot
|
||||
else:
|
||||
return qselect(i, left)
|
||||
Reference in New Issue
Block a user