-
-
Notifications
You must be signed in to change notification settings - Fork 50.7k
Add slowsort algorithm #14717
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
Open
dipeshrayg
wants to merge
1
commit into
TheAlgorithms:master
Choose a base branch
from
dipeshrayg:add_slowshort
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+76
−0
Open
Add slowsort algorithm #14717
Changes from all commits
Commits
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,76 @@ | ||
| """ | ||
| Slowsort | ||
|
|
||
| Slowsort is a humorous, deliberately inefficient sorting algorithm based on the | ||
| "multiply and surrender" paradigm — the opposite of divide and conquer. It was | ||
| invented by Andrei Broder and Jorge Stolfi and published in their 1986 paper | ||
| "Pessimal Algorithms and Simplexity Analysis". | ||
|
|
||
| The algorithm works recursively: | ||
| 1. Find the maximum of the first half and second half of the array separately. | ||
| 2. Compare those two maximums and place the larger one at the end. | ||
| 3. Recursively sort everything except the last element. | ||
|
|
||
| Slowsort is provably non-optimal and runs in superpolynomial time even on | ||
| average, making it slower than bogosort for small inputs but guaranteed to | ||
| terminate. | ||
|
|
||
| Time Complexity: O(n^(log n / 2)) — superpolynomial, worse than any polynomial | ||
| Space Complexity: O(log n) due to recursion stack | ||
|
|
||
| Reference: | ||
| https://en.wikipedia.org/wiki/Slowsort | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
|
|
||
| def slowsort(arr: list[int], low: int, high: int) -> None: | ||
| """ | ||
| Recursively sort arr[low..high] in place using the slowsort algorithm. | ||
|
|
||
| Args: | ||
| arr: The list of integers to sort (modified in place). | ||
| low: The starting index of the subarray to sort. | ||
| high: The ending index of the subarray to sort (inclusive). | ||
|
|
||
| >>> a = [5, 3, 8, 1, 9, 2] | ||
| >>> slowsort(a, 0, len(a) - 1) | ||
| >>> a | ||
| [1, 2, 3, 5, 8, 9] | ||
|
|
||
| >>> b = [1] | ||
| >>> slowsort(b, 0, 0) | ||
| >>> b | ||
| [1] | ||
|
|
||
| >>> c = [4, 4, 4] | ||
| >>> slowsort(c, 0, len(c) - 1) | ||
| >>> c | ||
| [4, 4, 4] | ||
|
|
||
| >>> d = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] | ||
| >>> slowsort(d, 0, len(d) - 1) | ||
| >>> d | ||
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
| """ | ||
| if low >= high: | ||
| return | ||
| mid = (low + high) // 2 | ||
| slowsort(arr, low, mid) | ||
| slowsort(arr, mid + 1, high) | ||
| if arr[mid] > arr[high]: | ||
| arr[mid], arr[high] = arr[high], arr[mid] | ||
| slowsort(arr, low, high - 1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
|
|
||
| user_input = input("Enter numbers separated by commas: ").strip() | ||
| unsorted = [int(x) for x in user_input.split(",")] | ||
| print(f"Unsorted: {unsorted}") | ||
| slowsort(unsorted, 0, len(unsorted) - 1) | ||
| print(f"Sorted: {unsorted}") | ||
Oops, something went wrong.
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.
I have one minor suggestion regarding the user input block at the bottom (72-76):
If a user enters an empty string or non-integer values, [int(x) for x in user_input.split(",")] will raise a ValueError.You can make this more robust like -Wrap the input section in a try-except block to handle invalid inputs gracefully.Great job overall! Let me know what you think.