1
1
"""
2
- A Python implementation of the Median of Medians algorithm to select pivots for QuickSelect,
3
- which is efficient for calculating the value that would appear in the index of a list if it
4
- would be sorted, even if it is not already sorted. Search in time complexity O(n) at any rank
2
+ A Python implementation of the Median of Medians algorithm
3
+ to select pivots for quick_select, which is efficient for
4
+ calculating the value that would appear in the index of a
5
+ list if it would be sorted, even if it is not already
6
+ sorted. Search in time complexity O(n) at any rank
5
7
deterministically
6
8
https://en.wikipedia.org/wiki/Median_of_medians
7
9
"""
8
10
11
+ """
12
+ >>> quick_select([2, 4, 5, 7, 899, 54, 32], 5)
13
+ 32
14
+ >>> quick_select([2, 4, 5, 7, 899, 54, 32], 1)
15
+ 2
16
+ >>> quick_select([5, 4, 3, 2], 2)
17
+ 3
18
+ >>> quick_select([3, 5, 7, 10, 2, 12], 3)
19
+ 5
20
+ """
9
21
10
- def MedianofFive (arr : list ) -> int :
22
+ def median_of_five (arr :list ) -> int :
23
+ """
24
+ >>> median_of_five([2, 4, 5, 7, 899])
25
+ 5
26
+ >>> median_of_five([5, 7, 899, 54, 32])
27
+ 32
28
+ >>> median_of_five([5, 4, 3, 2])
29
+ 3
30
+ >>> median_of_five([3, 5, 7, 10, 2])
31
+ 5
32
+ """
11
33
"""
12
34
Return the median of the input list
13
35
:param arr: Array to find median of
14
36
:return: median of arr
15
37
"""
16
- arr = sorted (arr )
17
- return arr [len (arr ) // 2 ]
18
-
38
+ arr = sorted (arr )
39
+ return arr [len (arr )// 2 ]
19
40
20
- def MedianofMedians (arr : list ) -> int :
41
+ def median_of_medians (arr :list ) -> int :
21
42
"""
22
43
Return a pivot to partition data on by calculating
23
44
Median of medians of input data
24
45
:param arr: The data to be sorted (a list)
25
46
:param k: The rank to be searched
26
47
:return: element at rank k
27
48
"""
28
- if len (arr ) <= 5 :
29
- return MedianofFive (arr )
49
+ if len (arr ) <= 5 : return median_of_five (arr )
30
50
medians = []
31
- i = 0
32
- while i < len (arr ):
33
- if (i + 4 ) <= len (arr ):
34
- medians .append (MedianofFive (arr [i :].copy ()))
35
- else :
36
- medians .append (MedianofFive (arr [i : i + 5 ].copy ()))
37
- i += 5
38
- return MedianofMedians (medians )
39
-
51
+ i = 0
52
+ while i < len (arr ):
53
+ if (i + 4 ) <= len (arr ): medians .append (median_of_five (arr [i :].copy ()))
54
+ else : medians .append (median_of_five (arr [i :i + 5 ].copy ()))
55
+ i += 5
56
+ return median_of_medians (medians )
40
57
41
- def QuickSelect (arr : list , k : int ) -> int :
58
+ def quick_select (arr :list , k :int ) -> int :
42
59
"""
43
- >>> QuickSelect ([2, 4, 5, 7, 899, 54, 32], 5)
60
+ >>> quick_select ([2, 4, 5, 7, 899, 54, 32], 5)
44
61
32
45
- >>> QuickSelect ([2, 4, 5, 7, 899, 54, 32], 1)
62
+ >>> quick_select ([2, 4, 5, 7, 899, 54, 32], 1)
46
63
2
47
- >>> QuickSelect ([5, 4, 3, 2], 2)
64
+ >>> quick_select ([5, 4, 3, 2], 2)
48
65
3
49
- >>> QuickSelect ([3, 5, 7, 10, 2, 12], 3)
66
+ >>> quick_select ([3, 5, 7, 10, 2, 12], 3)
50
67
5
51
68
"""
52
69
@@ -59,15 +76,14 @@ def QuickSelect(arr: list, k: int) -> int:
59
76
"""
60
77
61
78
# Invalid Input
62
- if k > len (arr ):
63
- return None
79
+ if k > len (arr ):
80
+ return - 1
64
81
65
82
# x is the estimated pivot by median of medians algorithm
66
- x = MedianofMedians (arr )
83
+ x = median_of_medians (arr )
67
84
left = []
68
85
right = []
69
86
check = False
70
- smaller = 0
71
87
for i in range (len (arr )):
72
88
if arr [i ] < x :
73
89
left .append (arr [i ])
@@ -77,11 +93,8 @@ def QuickSelect(arr: list, k: int) -> int:
77
93
check = True
78
94
else :
79
95
right .append (arr [i ])
80
- rankX = len (left ) + 1
81
- if rankX == k :
82
- answer = x
83
- elif rankX > k :
84
- answer = QuickSelect (left , k )
85
- elif rankX < k :
86
- answer = QuickSelect (right , k - rankX )
87
- return answer
96
+ rank_X = len (left ) + 1
97
+ if (rank_X == k ): answer = x
98
+ elif rank_X > k : answer = quick_select (left ,k )
99
+ elif rank_X < k : answer = quick_select (right ,k - rank_X )
100
+ return answer ;
0 commit comments