-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
added bogobogosort #1258
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
added bogobogosort #1258
Changes from 1 commit
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,54 @@ | ||||
""" | ||||
Python implementation of bogobogosort, a "sorting algorithm | ||||
designed not to succeed before the heat death of the universe | ||||
on any sizable list" - https://en.wikipedia.org/wiki/Bogosort. | ||||
|
||||
Author: WilliamHYZhang | ||||
""" | ||||
|
||||
import random | ||||
|
||||
|
||||
def bogo_bogo_sort(collection): | ||||
""" | ||||
returns the collection sorted in ascending order | ||||
:param collection: list of comparable items | ||||
:return: the list sorted in ascending order | ||||
|
||||
Examples: | ||||
>>> bogo_bogo_sort([0, 5, 3, 2, 2]) | ||||
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. At least one test should have at least one:
What happens when we bogo_bogo_sort(list("Hello!")) ? 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. Test #2 has negative integer, does that satisfy the requirement? 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. Mixing pos int, pos float, zero, neg int, neg float in a single test was what I was looking for. Python/machine_learning/dbscan/dbscan.py Line 14 in 4617aa7
|
||||
[0, 2, 2, 3, 5] | ||||
>>> bogo_bogo_sort([-2, -5, -45]) | ||||
[-45, -5, -2] | ||||
>>> bogo_bogo_sort([420, 69]) | ||||
[69, 420] | ||||
""" | ||||
|
||||
def is_sorted(collection): | ||||
if len(collection) == 1: | ||||
return True | ||||
|
||||
clone = collection.copy() | ||||
while True: | ||||
random.shuffle(clone) | ||||
ordered = bogo_bogo_sort(clone[:-1]) | ||||
if clone[len(clone) - 1] >= max(ordered): | ||||
break | ||||
|
||||
for i in range(len(ordered)): | ||||
clone[i] = ordered[i] | ||||
|
||||
for i in range(len(collection)): | ||||
if clone[i] != collection[i]: | ||||
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_bogo_sort(unsorted)) |
Uh oh!
There was an error while loading. Please reload this page.