Skip to content

Commit 354da61

Browse files
Added python bogosort
1 parent 7b5d252 commit 354da61

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

chapters/sorting_searching/bogo/bogo_sort.md

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

0 commit comments

Comments
 (0)