Skip to content
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 Sorting Algorithm (Bogo Sort) #36

Merged
merged 3 commits into from
Oct 3, 2022
Merged
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
28 changes: 28 additions & 0 deletions Sorting/Bogo Sort/in_Python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Contributed by Deepanshu Arya <[email protected]>
bogosort (also known as permutation sort, stupid sort, or slowsort) is a sorting algorithm
based on the generate and test paradigm. The function successively generates permutations
of its input until it finds one that is sorted. It is not considered useful for sorting,
but may be used for educational purposes, to contrast it with more efficient algorithms.

Time Complexity:
Worst Case : O(∞) (since this algorithm has no upper bound)
Average Case: O(n*n!)
Best Case : O(n)(when array given is already sorted)
Auxiliary Space: O(1)
"""
from random import shuffle

def is_sorted(data) -> bool:
"""Determine whether the data is sorted."""
return all(a <= b for a, b in zip(data, data[1:]))

def bogosort(data) -> list:
"""Shuffle data until sorted."""
while not is_sorted(data):
shuffle(data)
return data

#driver code
data = [int(x) for x in input().split()]
print(bogosort(data))