Skip to content

Commit 5ba4b9f

Browse files
authored
Update median_of_medians.py
Made the changes needed
1 parent d8f539e commit 5ba4b9f

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

Diff for: searches/median_of_medians.py

+49-36
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,69 @@
11
"""
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
57
deterministically
68
https://en.wikipedia.org/wiki/Median_of_medians
79
"""
810

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+
"""
921

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+
"""
1133
"""
1234
Return the median of the input list
1335
:param arr: Array to find median of
1436
:return: median of arr
1537
"""
16-
arr = sorted(arr)
17-
return arr[len(arr) // 2]
18-
38+
arr=sorted(arr)
39+
return arr[len(arr)//2]
1940

20-
def MedianofMedians(arr: list) -> int:
41+
def median_of_medians(arr:list) -> int:
2142
"""
2243
Return a pivot to partition data on by calculating
2344
Median of medians of input data
2445
:param arr: The data to be sorted (a list)
2546
:param k: The rank to be searched
2647
:return: element at rank k
2748
"""
28-
if len(arr) <= 5:
29-
return MedianofFive(arr)
49+
if len(arr) <= 5: return median_of_five(arr)
3050
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)
4057

41-
def QuickSelect(arr: list, k: int) -> int:
58+
def quick_select(arr:list, k:int) -> int:
4259
"""
43-
>>> QuickSelect([2, 4, 5, 7, 899, 54, 32], 5)
60+
>>> quick_select([2, 4, 5, 7, 899, 54, 32], 5)
4461
32
45-
>>> QuickSelect([2, 4, 5, 7, 899, 54, 32], 1)
62+
>>> quick_select([2, 4, 5, 7, 899, 54, 32], 1)
4663
2
47-
>>> QuickSelect([5, 4, 3, 2], 2)
64+
>>> quick_select([5, 4, 3, 2], 2)
4865
3
49-
>>> QuickSelect([3, 5, 7, 10, 2, 12], 3)
66+
>>> quick_select([3, 5, 7, 10, 2, 12], 3)
5067
5
5168
"""
5269

@@ -59,15 +76,14 @@ def QuickSelect(arr: list, k: int) -> int:
5976
"""
6077

6178
# Invalid Input
62-
if k > len(arr):
63-
return None
79+
if k>len(arr):
80+
return -1
6481

6582
# x is the estimated pivot by median of medians algorithm
66-
x = MedianofMedians(arr)
83+
x = median_of_medians(arr)
6784
left = []
6885
right = []
6986
check = False
70-
smaller = 0
7187
for i in range(len(arr)):
7288
if arr[i] < x:
7389
left.append(arr[i])
@@ -77,11 +93,8 @@ def QuickSelect(arr: list, k: int) -> int:
7793
check = True
7894
else:
7995
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

Comments
 (0)