Skip to content

Commit f8899cd

Browse files
Update quick_select.py
1 parent ab31f1a commit f8899cd

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

searches/quick_select.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ def quick_select(items: list, index: int):
6262
return quick_select(larger, index - (m + count))
6363

6464

65-
def median(data: list):
66-
"""One common application of Quickselect is finding the median, which is
67-
the middle element (or average of the two middle elements) in a dataset. It
68-
works efficiently on unsorted lists by partially sorting the data without
65+
def median(items: list):
66+
"""
67+
One common application of Quickselect is finding the median, which is
68+
the middle element (or average of the two middle elements) in a sorted dataset.
69+
It works efficiently on unsorted lists by partially sorting the data without
6970
fully sorting the entire list.
7071
7172
>>> import random
@@ -80,14 +81,15 @@ def median(data: list):
8081
8182
>>> d = [2, 2, 3, 9, 9, 9]
8283
>>> random.shuffle(d)
84+
>>> d
85+
[2, 2, 3, 9, 9, 9]
8386
>>> median(d)
8487
6.0
85-
8688
"""
87-
mid, rest = divmod(len(data), 2)
88-
if rest:
89-
return quick_select(data, mid)
89+
mid, rest = divmod(len(items), 2)
90+
if rest != 0:
91+
return quick_select(items=items, index=mid)
9092
else:
91-
low_mid = quick_select(data, mid - 1)
92-
high_mid = quick_select(data, mid)
93+
low_mid = quick_select(items=items, index=mid - 1)
94+
high_mid = quick_select(items=items, index=mid)
9395
return (low_mid + high_mid) / 2

0 commit comments

Comments
 (0)