Skip to content

Commit 15535cb

Browse files
Python implementaiton of bubble sort is now shown, also tab fixes
1 parent 2826032 commit 15535cb

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

chapters/sorting_searching/bubble/bubble_sort.md

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

64

75
def bubble_sort(array):
8-
len_array = len(array)
9-
for i in range(len_array):
10-
for j in range(len_array - i - 1):
11-
if(array[j] > array[j+1]):
12-
array[j], array[j+1] = array[j+1], array[j] #swap elements in the list
13-
6+
len_array = len(array)
7+
for i in range(len_array):
8+
for j in range(len_array - i - 1):
9+
if(array[j] > array[j+1]):
10+
array[j], array[j+1] = array[j+1], array[j] #swap elements in the list
1411

1512
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-
13+
number = [random.randint(0, 1000) for _ in range(10)]
14+
print("Before Sorting {}".format(number))
15+
bubble_sort(number)
16+
print("After Sorting {}".format(number))
2117

22-
if __name__ == "__main__":
23-
main()
18+
main()

0 commit comments

Comments
 (0)