Skip to content

Create flashsort.py #11952

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 2 commits 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
48 changes: 48 additions & 0 deletions sorts/flashsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 5 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 5 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

sorts/flashsort.py:5: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.

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 24 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

sorts/flashsort.py:24: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

For manual testing, run:
python bogo_sort.py
"""

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

Check failure on line 34 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file sorts/flashsort.py, please provide doctest for the function is_sorted

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