Skip to content

Commit 1d86c1e

Browse files
Added python bogosort
1 parent 3ef05bf commit 1d86c1e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

chapters/sorting_searching/bogo/bogo_sort.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ In code, it looks something like this:
2424
{% sample lang="js" %}
2525
[import:1-16, lang:"javascript"](code/js/bogo.js)
2626
{% sample lang="hs" %}
27+
[import:1-3, lang:"python"](code/python/bogo.py)
28+
{% sample lang="py" %}
2729
[import, lang:"haskell"](code/haskell/bogoSort.hs)
2830
{% sample lang="cpp" %}
2931
[import, lang:"c_cpp"](code/c++/bogosort.cpp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from __future__ import print_function
2+
import random
3+
4+
def is_sorted(a):
5+
for i in range(len(a)-1):
6+
if a[i+1] < a[i]:
7+
return False
8+
return True
9+
10+
11+
def bogo_sort(a):
12+
while not is_sorted(a):
13+
random.shuffle(a)
14+
15+
a = [1., 3, 2, 4]
16+
bogo_sort(a)
17+
print(a)
18+

0 commit comments

Comments
 (0)