2017-10-09 21:26:27 +00:00
|
|
|
import random
|
2018-10-17 21:28:57 +00:00
|
|
|
|
2017-10-09 21:26:27 +00:00
|
|
|
"""
|
|
|
|
A python implementation of the quick select algorithm, which is efficient for calculating the value that would appear in the index of a list if it would be sorted, even if it is not already sorted
|
|
|
|
https://en.wikipedia.org/wiki/Quickselect
|
|
|
|
"""
|
|
|
|
def _partition(data, pivot):
|
|
|
|
"""
|
|
|
|
Three way partition the data into smaller, equal and greater lists,
|
|
|
|
in relationship to the pivot
|
|
|
|
:param data: The data to be sorted (a list)
|
|
|
|
:param pivot: The value to partition the data on
|
|
|
|
:return: Three list: smaller, equal and greater
|
|
|
|
"""
|
|
|
|
less, equal, greater = [], [], []
|
|
|
|
for element in data:
|
|
|
|
if element.address < pivot.address:
|
|
|
|
less.append(element)
|
|
|
|
elif element.address > pivot.address:
|
|
|
|
greater.append(element)
|
|
|
|
else:
|
|
|
|
equal.append(element)
|
|
|
|
return less, equal, greater
|
|
|
|
|
2018-10-17 21:28:57 +00:00
|
|
|
def quickSelect(list, k):
|
2017-10-09 21:26:27 +00:00
|
|
|
#k = len(list) // 2 when trying to find the median (index that value would be when list is sorted)
|
2018-10-17 21:28:57 +00:00
|
|
|
smaller = []
|
|
|
|
larger = []
|
|
|
|
pivot = random.randint(0, len(list) - 1)
|
|
|
|
pivot = list[pivot]
|
|
|
|
count = 0
|
|
|
|
smaller, equal, larger =_partition(list, pivot)
|
|
|
|
count = len(equal)
|
|
|
|
m = len(smaller)
|
2017-10-09 21:26:27 +00:00
|
|
|
|
2018-10-17 21:28:57 +00:00
|
|
|
#k is the pivot
|
|
|
|
if m <= k < m + count:
|
2017-10-09 21:26:27 +00:00
|
|
|
return pivot
|
|
|
|
# must be in smaller
|
2018-10-17 21:28:57 +00:00
|
|
|
elif m > k:
|
2017-10-09 21:26:27 +00:00
|
|
|
return quickSelect(smaller, k)
|
|
|
|
#must be in larger
|
2018-10-17 21:28:57 +00:00
|
|
|
else:
|
2017-10-09 21:26:27 +00:00
|
|
|
return quickSelect(larger, k - (m + count))
|