-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
kth order statistic divide and conquer algorithm #3690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ab44c47
kth order statistics divide and conquer algorithm
choyiny b1046c8
add explanation of algorithm.
choyiny 7dfee7e
fix PEP8 line too long error
choyiny f972e4a
update order to be compliant to isort
choyiny 8e003dd
add doctest
choyiny 082d227
make file black compliant
choyiny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
Find the kth smallest element in linear time using divide and conquer. | ||
Recall we can do this trivially in O(nlogn) time. Sort the list and | ||
access kth element in constant time. | ||
|
||
This is a divide and conquer algorithm that can find a solution in O(n) time. | ||
|
||
For more information of this algorithm: | ||
https://web.stanford.edu/class/archive/cs/cs161/cs161.1138/lectures/08/Small08.pdf | ||
""" | ||
from random import choice | ||
from typing import List | ||
|
||
|
||
def random_pivot(lst): | ||
""" | ||
Choose a random pivot for the list. | ||
We can use a more sophisticated algorithm here, such as the median-of-medians | ||
algorithm. | ||
""" | ||
return choice(lst) | ||
|
||
|
||
def kth_number(lst: List[int], k: int) -> int: | ||
""" | ||
Return the kth smallest number in lst. | ||
>>> kth_number([2, 1, 3, 4, 5], 3) | ||
3 | ||
>>> kth_number([2, 1, 3, 4, 5], 1) | ||
1 | ||
>>> kth_number([2, 1, 3, 4, 5], 5) | ||
5 | ||
>>> kth_number([3, 2, 5, 6, 7, 8], 2) | ||
3 | ||
>>> kth_number([25, 21, 98, 100, 76, 22, 43, 60, 89, 87], 4) | ||
43 | ||
""" | ||
# pick a pivot and separate into list based on pivot. | ||
pivot = random_pivot(lst) | ||
|
||
# partition based on pivot | ||
# linear time | ||
small = [e for e in lst if e < pivot] | ||
big = [e for e in lst if e > pivot] | ||
|
||
# if we get lucky, pivot might be the element we want. | ||
# we can easily see this: | ||
# small (elements smaller than k) | ||
# + pivot (kth element) | ||
# + big (elements larger than k) | ||
if len(small) == k - 1: | ||
return pivot | ||
# pivot is in elements bigger than k | ||
elif len(small) < k - 1: | ||
return kth_number(big, k - len(small) - 1) | ||
# pivot is in elements smaller than k | ||
else: | ||
return kth_number(small, k) | ||
poyea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.