forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbogo_sort.py
More file actions
32 lines (25 loc) · 740 Bytes
/
bogo_sort.py
File metadata and controls
32 lines (25 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import random
def bogo_sort(arr, simulation=False):
"""Bogo Sort
Best Case Complexity: O(n)
Worst Case Complexity: O(∞)
Average Case Complexity: O(n(n-1)!)
"""
iteration = 0
if simulation:
print("iteration",iteration,":",*arr)
def is_sorted(arr):
#check the array is inorder
i = 0
arr_len = len(arr)
while i+1 < arr_len:
if arr[i] > arr[i+1]:
return False
i += 1
return True
while not is_sorted(arr):
random.shuffle(arr)
if simulation:
iteration = iteration + 1
print("iteration",iteration,":",*arr)
return arr