Initial solution to homework

This commit is contained in:
2019-09-26 23:30:53 -07:00
parent 677d9c277b
commit 5c43ffb602
3 changed files with 103 additions and 0 deletions

15
qselect.py Normal file
View 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)