Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
delete 'address' and add input validation
  • Loading branch information
Vincent Hu committed May 15, 2019
commit eb0e34f85445a522e074a5e09df33e960a596997
11 changes: 8 additions & 3 deletions searches/quick_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ def _partition(data, pivot):
"""
less, equal, greater = [], [], []
for element in data:
if element.address < pivot.address:
if element < pivot:
less.append(element)
elif element.address > pivot.address:
elif element > pivot:
greater.append(element)
else:
equal.append(element)
return less, equal, greater

def quickSelect(list, k):
#k = len(list) // 2 when trying to find the median (index that value would be when list is sorted)

#invalid input
if k>=len(list) or k<0:
return None

smaller = []
larger = []
pivot = random.randint(0, len(list) - 1)
Expand All @@ -41,4 +46,4 @@ def quickSelect(list, k):
return quickSelect(smaller, k)
#must be in larger
else:
return quickSelect(larger, k - (m + count))
return quickSelect(larger, k - (m + count))