-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-12.48.py
47 lines (38 loc) · 993 Bytes
/
c-12.48.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
37
38
39
40
41
42
43
44
45
46
47
"""
Our quick-select implementation can be made more space-efficient by initially computing
only the counts for sets L, E, and G, creating only the new subset that will be needed
for recursion. Implement such a version.
"""
import random
def quick_select(S, kth):
n = len(S)
if n < 2:
return S[0]
pivot = random.choice(S)
l, g, e = 0, 0, 0
for el in S:
if el < pivot:
l += 1
elif el > pivot:
g += 1
else:
e += 1
if kth <= l:
L = [el for el in S if el < pivot]
return quick_select(L, kth)
elif kth <= l + e:
return pivot
else:
G = [el for el in S if el > pivot]
kth -= l + e
return quick_select(G, kth)
if __name__ == "__main__":
S = [3, 6, 1, 2, 4, 6, 7]
kth = 5
assert quick_select(S, kth) == 6
S = [7, 7, 7]
kth = 3
assert quick_select(S, kth) == 7
S = [0]
kth = 1
assert quick_select(S, kth) == 0