-
-
Notifications
You must be signed in to change notification settings - Fork 50.6k
Create bozo_sort.py #14303
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
base: master
Are you sure you want to change the base?
Create bozo_sort.py #14303
Changes from all commits
8a335b6
2a55aa2
bf619d9
8c1ab9a
983c289
7c4cc3a
d35bb68
1f1d3f7
bd2406e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """ | ||
| This is a pure Python implementation of the bozosort algorithm. | ||
|
|
||
| Bozosort chooses two elements at random and swaps them. It does | ||
| this repeatedly until the list is sorted. | ||
|
|
||
| For more info, check "Bozosort": https://en.wikipedia.org/wiki/Bogosort#Related_algorithms | ||
|
|
||
| For doctests run following command: | ||
| python -m doctest -v bozo_sort.py | ||
| or | ||
| python3 -m doctest -v bozo_sort.py | ||
|
|
||
| For manual testing run: | ||
| python bozo_sort.py | ||
| """ | ||
|
|
||
| import random | ||
|
|
||
|
|
||
| def bozo_sort(array: list) -> list: | ||
| """ | ||
| Pure Python implementation of the bozosort algorithm. | ||
| Examples: | ||
| >>> bozo_sort([1, 14, 20, 9, 5]) | ||
| [1, 5, 9, 14, 20] | ||
| >>> bozo_sort([8, 16, 0, 4, 10]) | ||
| [0, 4, 8, 10, 16] | ||
| """ | ||
|
|
||
| def is_sorted(array: list) -> bool: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| for i, n in enumerate(array): | ||
| if i < len(array) - 1 and n > array[i + 1]: | ||
| return False | ||
| return True | ||
|
|
||
| while not is_sorted(array): | ||
| index_a = random.randint(0, len(array) - 1) | ||
| index_b = random.randint(0, len(array) - 1) | ||
|
|
||
| array[index_a], array[index_b] = array[index_b], array[index_a] | ||
|
|
||
| return array | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| user_array = input("Enter numbers separated by spaces: ") | ||
| unsorted = [int(i) for i in user_array.split(" ")] | ||
|
|
||
| print(bozo_sort(unsorted)) | ||
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.
As there is no test file in this pull request nor any test function or class in the file
sorts/bozo_sort.py, please provide doctest for the functionis_sorted