You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
2
+
A Python implementation of the quick select algorithm, which is efficient for
3
+
calculating the value that would appear in the index of a list if it would be
4
+
sorted, even if it is not already sorted
5
5
https://en.wikipedia.org/wiki/Quickselect
6
6
"""
7
+
importrandom
7
8
8
9
9
-
def_partition(data, pivot):
10
+
def_partition(data: list, pivot)->tuple:
10
11
"""
11
12
Three way partition the data into smaller, equal and greater lists,
12
13
in relationship to the pivot
@@ -25,28 +26,37 @@ def _partition(data, pivot):
25
26
returnless, equal, greater
26
27
27
28
28
-
defquickSelect(list, k):
29
-
# k = len(list) // 2 when trying to find the median (index that value would be when list is sorted)
29
+
defquick_select(items: list, index: int):
30
+
"""
31
+
>>> quick_select([2, 4, 5, 7, 899, 54, 32], 5)
32
+
54
33
+
>>> quick_select([2, 4, 5, 7, 899, 54, 32], 1)
34
+
4
35
+
>>> quick_select([5, 4, 3, 2], 2)
36
+
4
37
+
>>> quick_select([3, 5, 7, 10, 2, 12], 3)
38
+
7
39
+
"""
40
+
# index = len(items) // 2 when trying to find the median
0 commit comments