File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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 ]),
You can’t perform that action at this time.
0 commit comments