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
bpo-21592: Implement non recursive quickselect for statistics.py #6715
Conversation
904fc16
to
e50c95d
Compare
ede5d46
to
71f350d
Compare
Lib/statistics.py
Outdated
@@ -373,15 +393,20 @@ def median(data): | |||
4.0 | |||
|
|||
""" | |||
data = sorted(data) | |||
data = [d for d in data] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this not calling list(data)
?
Lib/statistics.py
Outdated
else: | ||
i = n//2 | ||
return (data[i - 1] + data[i])/2 | ||
return _quick_select(data, 0, n-1, pos-1)/2 + _quick_select(data, 0, n-1, pos)/2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be surprised if there wasn't a way to avoid doing the whole expensive operation twice.
@scoder Thank you for review, I updated it. |
I read some of not merged PR for this issue.
All of them are recursive versions which can cause a stack overflow.
This version is an iterative version which can avoid this issue.
https://bugs.python.org/issue21592