Skip to content

Commit 500751e

Browse files
Added python bogosort
1 parent e442eb1 commit 500751e

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
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:5-14, 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,20 @@
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+
12+
def bogo_sort(a):
13+
while not is_sorted(a):
14+
random.shuffle(a)
15+
16+
17+
a = [1., 3, 2, 4]
18+
bogo_sort(a)
19+
print(a)
20+

chapters/sorting_searching/bubble/bubble_sort.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
1717
{% sample lang="js" %}
1818
[import:1-11, lang:"javascript"](code/js/bubble.js)
1919
{% sample lang="hs" %}
20+
[import:5-10, lang:"python"](code/python/bubblesort.py)
21+
{% sample lang="py" %}
2022
[import, lang:"haskell"](code/haskell/bubbleSort.hs)
2123
{% sample lang="cpp" %}
2224
[import, lang:"c_cpp"](code/c++/bubblesort.cpp)
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#!usr/bin/python3
2-
3-
#import section
1+
from __future__ import print_function
42
import random
53

64

@@ -12,12 +10,8 @@ def bubble_sort(array):
1210
array[j], array[j+1] = array[j+1], array[j] #swap elements in the list
1311

1412

15-
def main():
16-
number = [random.randint(0, 10000) for _ in range(10)]
17-
print("Before Sorting {}".format(number))
18-
bubble_sort(number)
19-
print("After Sorting {}".format(number))
20-
2113

22-
if __name__ == "__main__":
23-
main()
14+
number = [random.randint(0, 1000) for _ in range(10)]
15+
print("Before Sorting {}".format(number))
16+
bubble_sort(number)
17+
print("After Sorting {}".format(number))

0 commit comments

Comments
 (0)