Skip to content

Commit ce4670d

Browse files
committed
bogo-sort
1 parent 7a28184 commit ce4670d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

python/sorting/bogo-sort.py

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

0 commit comments

Comments
 (0)