Skip to content

Commit f69598b

Browse files
Bubble Sort in Python
1 parent 6cbe2d5 commit f69598b

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

git

Whitespace-only changes.

sorting/bubble-sort-improvement.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of DSA course available on CourseGalaxy.com
3+
4+
def bubble_sort(a):
5+
for x in range(len(a)-1,0,-1):
6+
swaps=0
7+
for j in range(x):
8+
if a[j]>a[j+1]:
9+
a[j],a[j+1] = a[j+1],a[j]
10+
swaps+=1
11+
if swaps == 0:
12+
break
13+
14+
####################################
15+
16+
list1 = [6,3,1,5,9,8]
17+
bubble_sort(list1)
18+
print(list1)
19+
20+
list2 = [2,3,5,39,11,8,9,166,45,23]
21+
bubble_sort(list2)
22+
print(list2)
23+
24+
list3 = [1,2,3,4,5,6,7,8,9,10]
25+
bubble_sort(list3)
26+
print(list3)
27+
28+
list4 = [10,9,8,7,6,5,4,3,2,1]
29+
bubble_sort(list4)
30+
print(list4)
31+
32+
list5 = [4]
33+
bubble_sort(list5)
34+
print(list5)
35+
36+
#####################################

sorting/bubble-sort.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of DSA course available on CourseGalaxy.com
3+
4+
def bubble_sort(a):
5+
for x in range(len(a)-1,0,-1):
6+
for j in range(x):
7+
if a[j]>a[j+1]:
8+
a[j],a[j+1] = a[j+1],a[j]
9+
10+
11+
####################################
12+
13+
list1 = [6,3,1,5,9,8]
14+
bubble_sort(list1)
15+
print(list1)
16+
17+
list2 = [2,3,5,39,11,8,9,166,45,23]
18+
bubble_sort(list2)
19+
print(list2)
20+
21+
list3 = [1,2,3,4,5,6,7,8,9,10]
22+
bubble_sort(list3)
23+
print(list3)
24+
25+
list4 = [10,9,8,7,6,5,4,3,2,1]
26+
bubble_sort(list4)
27+
print(list4)
28+
29+
list5 = [4]
30+
bubble_sort(list5)
31+
print(list5)
32+
33+
#####################################

0 commit comments

Comments
 (0)