-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Create slowsort.py #3865
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
Create slowsort.py #3865
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aa8edea
Create slowsort.py
DjaouadNM 49d85dd
Update slowsort.py
DjaouadNM a4c2c74
Update slowsort.py
DjaouadNM 483ef06
Update slowsort.py
DjaouadNM 5d2fc3f
Update slowsort.py
DjaouadNM 35e95a5
Update slowsort.py
DjaouadNM 18bf22c
Update slowsort.py
DjaouadNM a1f6d6e
Update slowsort.py
DjaouadNM 96f68a0
Update slowsort.py
DjaouadNM b7f169d
Update slowsort.py
DjaouadNM b3bff16
Update slowsort.py
DjaouadNM 9687f27
Update slowsort.py
DjaouadNM 3f30788
Update slowsort.py
DjaouadNM 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,61 @@ | ||
""" | ||
Slowsort is a sorting algorithm. It is of humorous nature and not useful. | ||
It's based on the principle of multiply and surrender, | ||
a tongue-in-cheek joke of divide and conquer. | ||
It was published in 1986 by Andrei Broder and Jorge Stolfi | ||
in their paper Pessimal Algorithms and Simplexity Analysis | ||
(a parody of optimal algorithms and complexity analysis). | ||
|
||
Source: https://en.wikipedia.org/wiki/Slowsort | ||
""" | ||
|
||
from typing import Optional | ||
|
||
|
||
def slowsort(sequence: list, i: Optional[int] = None, j: Optional[int] = None) -> None: | ||
""" | ||
Sorts sequence[i..j] (both inclusive) in-place. | ||
i defaults to 0 if not given. | ||
j defaults to len(sequence) - 1 if not given. | ||
It returns None. | ||
>>> sequence = [1, 6, 2, 5, 3, 4, 4, 5]; slowsort(sequence); sequence | ||
[1, 2, 3, 4, 4, 5, 5, 6] | ||
>>> sequence = []; slowsort(sequence); sequence | ||
[] | ||
>>> sequence = [2]; slowsort(sequence); sequence | ||
[2] | ||
>>> sequence = [1, 2, 3, 4]; slowsort(sequence); sequence | ||
[1, 2, 3, 4] | ||
>>> sequence = [4, 3, 2, 1]; slowsort(sequence); sequence | ||
[1, 2, 3, 4] | ||
>>> sequence = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; slowsort(sequence, 2, 7); sequence | ||
[9, 8, 2, 3, 4, 5, 6, 7, 1, 0] | ||
>>> sequence = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; slowsort(sequence, j = 4); sequence | ||
[5, 6, 7, 8, 9, 4, 3, 2, 1, 0] | ||
>>> sequence = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; slowsort(sequence, i = 5); sequence | ||
[9, 8, 7, 6, 5, 0, 1, 2, 3, 4] | ||
""" | ||
if i is None: | ||
i = 0 | ||
|
||
if j is None: | ||
j = len(sequence) - 1 | ||
|
||
if i >= j: | ||
return | ||
|
||
m = (i + j) // 2 | ||
|
||
slowsort(sequence, i, m) | ||
slowsort(sequence, m + 1, j) | ||
|
||
if sequence[j] < sequence[m]: | ||
sequence[j], sequence[m] = sequence[m], sequence[j] | ||
|
||
slowsort(sequence, i, j - 1) | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
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.
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.
Please provide descriptive name for the parameter:
i
Please provide descriptive name for the parameter:
j