Skip to content

Commit 14a350f

Browse files
authored
Create bogosort.py
1 parent 8385b62 commit 14a350f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Sorting Algorithms/bogosort.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import random
2+
3+
# Sorts array a[0..n-1] using Bogo sort
4+
5+
6+
def bogo_sort(a):
7+
n = len(a)
8+
while (is_sorted(a) == False):
9+
shuffle(a)
10+
11+
# To check if array is sorted or not
12+
13+
14+
def is_sorted(a):
15+
n = len(a)
16+
for i in range(0, n - 1):
17+
if (a[i] > a[i + 1]):
18+
return False
19+
return True
20+
21+
# To generate permuatation of the array
22+
23+
24+
def shuffle(a):
25+
n = len(a)
26+
for i in range(0, n):
27+
r = random.randint(0, n - 1)
28+
a[i], a[r] = a[r], a[i]
29+
30+
31+
# Driver code to test above
32+
a = [3, 2, 4, 1, 0, 5]
33+
bogo_sort(a)
34+
print("Sorted array :")
35+
for i in range(len(a)):
36+
print("%d" % a[i]),

0 commit comments

Comments
 (0)