-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickselect.py
36 lines (26 loc) · 879 Bytes
/
quickselect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Quickselect
Get the k smallest items from a list.
The pivot is the last element of the selected range.
"""
def select(items: list, k: int, key=lambda x: x):
_recursive_select(items, k, 0, len(items) - 1, key)
return items[:k]
def _recursive_select(items, k, begin, end, key):
if begin >= end:
return
partition = _partition(items, begin, end, key)
if partition > k - 1:
_recursive_select(items, k, begin, partition - 1, key)
if partition < k - 1:
_recursive_select(items, k, partition + 1, end, key)
def _partition(items, begin, end, key) -> int:
i = begin
for j in range(begin, end + 1):
if key(items[j]) <= key(items[end]):
_swap(items, i, j)
i += 1
return i - 1
def _swap(items, idx_1, idx_2):
tmp = items[idx_1]
items[idx_1] = items[idx_2]
items[idx_2] = tmp