MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / bogo_sort

Function bogo_sort

sorts/bogo_sort.py:19–41  ·  view source on GitHub ↗

Pure implementation of the bogosort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending Examples: >>> bogo_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> bogo_sort([

(collection: list)

Source from the content-addressed store, hash-verified

17
18
19def bogo_sort(collection: list) -> list:
20 """Pure implementation of the bogosort algorithm in Python
21 :param collection: some mutable ordered collection with heterogeneous
22 comparable items inside
23 :return: the same collection ordered by ascending
24 Examples:
25 >>> bogo_sort([0, 5, 3, 2, 2])
26 [0, 2, 2, 3, 5]
27 >>> bogo_sort([])
28 []
29 >>> bogo_sort([-2, -5, -45])
30 [-45, -5, -2]
31 """
32
33 def is_sorted(collection: list) -> bool:
34 for i in range(len(collection) - 1):
35 if collection[i] > collection[i + 1]:
36 return False
37 return True
38
39 while not is_sorted(collection):
40 random.shuffle(collection)
41 return collection
42
43
44if __name__ == "__main__":

Callers 1

bogo_sort.pyFile · 0.85

Calls 1

is_sortedFunction · 0.70

Tested by

no test coverage detected