Skip to content

Create flashsort.py #11951

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions sorts/flashsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import random
from typing import List

Check failure on line 2 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

sorts/flashsort.py:2:1: UP035 `typing.List` is deprecated, use `list` instead

def bogo_sort(collection: List[int]) -> List[int]:

Check failure on line 4 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

sorts/flashsort.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 4 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

sorts/flashsort.py:4:27: UP006 Use `list` instead of `List` for type annotation

Check failure on line 4 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

sorts/flashsort.py:4:41: UP006 Use `list` instead of `List` for type annotation
"""Pure implementation of the bogosort algorithm in Python.

Bogosort generates random permutations until it guesses the correct one.

Check failure on line 8 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/flashsort.py:8:1: W293 Blank line contains whitespace
More info on: https://en.wikipedia.org/wiki/Bogosort

Args:
collection (List[int]): A mutable ordered collection with comparable items.

Returns:
List[int]: The same collection ordered by ascending.

Examples:
>>> bogo_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> bogo_sort([])
[]
>>> bogo_sort([-2, -5, -45])
[-45, -5, -2]

Raises:
ValueError: If the input is not a list of integers.

Note:
This is not an efficient sorting algorithm and is mainly used for educational purposes.

Check failure on line 29 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

sorts/flashsort.py:29:89: E501 Line too long (95 > 88)

For doctests, run the following command:
python -m doctest -v bogo_sort.py
or
python3 -m doctest -v bogo_sort.py

Check failure on line 35 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/flashsort.py:35:1: W293 Blank line contains whitespace
For manual testing, run:
python bogo_sort.py
"""

def is_sorted(collection: List[int]) -> bool:

Check failure on line 40 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

sorts/flashsort.py:40:31: UP006 Use `list` instead of `List` for type annotation
for i in range(len(collection) - 1):
if collection[i] > collection[i + 1]:
return False
return True

while not is_sorted(collection):
random.shuffle(collection)
return collection

if __name__ == "__main__":
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(bogo_sort(unsorted))
Loading