Skip to content

Commit af04f81

Browse files
adding comments on bogosort
1 parent 7115be0 commit af04f81

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Arrays-Sorting/src/BogoSort/BogoSort.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
1+
2+
'''
3+
bogosort is a highly ineffective sorting function based on the generate and test paradigm. The function successively generates permutations of its input until it finds one that is sorted. It is not useful for sorting, but may be used for educational purposes, to contrast it with more efficient algorithms.
4+
'''
5+
6+
17
import random
8+
9+
10+
'''
11+
Random shuffle elements in the array, by swaping random 2 elements in each loop iteration.
12+
13+
'''
214
def shuffle(array):
315
for i in range(len(array)-1, -1, -1):
416
j = random.randint(0, i)
517
array[i], array[j] = array[j], array[i]
18+
19+
'''
20+
Checks if the array is sorted, by having non-decreasing sequence.
21+
22+
'''
23+
624
def is_sorted(array):
725
for i in range(len(array) - 1):
826
if array[i] > array[i + 1]:
927
return False
1028
return True
29+
30+
31+
'''
32+
__main__ function of the sort.
33+
34+
'''
1135
def bogosort(array):
1236
while not is_sorted(array):
1337
shuffle(array)

0 commit comments

Comments
 (0)