Skip to content

Commit 18b9ce1

Browse files
jiwoogitgoswami-rahul
authored andcommitted
added simulation to bogo_sort.py (keon#355)
1 parent 27d53fb commit 18b9ce1

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

algorithms/sort/bogo_sort.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import random
22

3-
def bogo_sort(arr):
3+
def bogo_sort(arr, simulation=False):
44
"""Bogo Sort
55
Best Case Complexity: O(n)
66
Worst Case Complexity: O(∞)
77
Average Case Complexity: O(n(n-1)!)
88
"""
9+
10+
iteration = 0
11+
if simulation:
12+
print("iteration",iteration,":",*arr)
13+
914
def is_sorted(arr):
1015
#check the array is inorder
1116
i = 0
@@ -14,9 +19,14 @@ def is_sorted(arr):
1419
if arr[i] > arr[i+1]:
1520
return False
1621
i += 1
22+
23+
1724
return True
1825
while not is_sorted(arr):
1926
random.shuffle(arr)
27+
28+
if simulation:
29+
iteration = iteration + 1
30+
print("iteration",iteration,":",*arr)
31+
2032
return arr
21-
22-

0 commit comments

Comments
 (0)