forked from garyexplains/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort.py
26 lines (24 loc) · 809 Bytes
/
bubblesort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#
# Understanding Bubble Sort with Examples and Code!
# https://youtu.be/WOjc48sVo6E
#
def bubblesort(list):
swapped = True
while swapped:
print
print "New iteration..."
swapped = False
for i in range(len(list)-1):
if(list[i] > list[i+1]):
print "Index: " + str(i) + " - Swap " + str(list[i]) + " with " + str(list[i+1])
tmp = list [i]
list[i] = list[i+1]
list[i+1] = tmp
swapped = True
print list
print "Nothing left to swap. Done"
return list
l = [5,8,1,4,2]
print l
l = bubblesort(l)
print l