File tree 4 files changed +29
-11
lines changed
chapters/sorting_searching
4 files changed +29
-11
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,8 @@ In code, it looks something like this:
24
24
{% sample lang="js" %}
25
25
[ import:1-16, lang:"javascript"] ( code/js/bogo.js )
26
26
{% sample lang="hs" %}
27
+ [ import:5-14, lang:"python"] ( code/python/bogo.py )
28
+ {% sample lang="py" %}
27
29
[ import, lang:"haskell"] ( code/haskell/bogoSort.hs )
28
30
{% sample lang="cpp" %}
29
31
[ import, lang:"c_cpp"] ( code/c++/bogosort.cpp )
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change @@ -17,6 +17,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
17
17
{% sample lang="js" %}
18
18
[ import:1-11, lang:"javascript"] ( code/js/bubble.js )
19
19
{% sample lang="hs" %}
20
+ [ import:5-10, lang:"python"] ( code/python/bubblesort.py )
21
+ {% sample lang="py" %}
20
22
[ import, lang:"haskell"] ( code/haskell/bubbleSort.hs )
21
23
{% sample lang="cpp" %}
22
24
[ import, lang:"c_cpp"] ( code/c++/bubblesort.cpp )
Original file line number Diff line number Diff line change 1
- #!usr/bin/python3
2
-
3
- #import section
1
+ from __future__ import print_function
4
2
import random
5
3
6
4
@@ -12,12 +10,8 @@ def bubble_sort(array):
12
10
array [j ], array [j + 1 ] = array [j + 1 ], array [j ] #swap elements in the list
13
11
14
12
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
-
21
13
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 ))
You can’t perform that action at this time.
0 commit comments