forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.py
More file actions
19 lines (16 loc) · 829 Bytes
/
BubbleSort.py
File metadata and controls
19 lines (16 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#Bubble sort
#The main aim is to get the largest element and bring it to its original sorted position
#So the largest element will move to the bottom of the array
def bubble_sort(array):
for passes in range (1, len(array)):
#Note: we are subctracting passes from array lenght so thats why we start passes from 1 and not 0
#We can think about starting passes from 0 to len(array) but again the substraction wont work
for i in range(0, len(array)-passes):
if(array[i] > array[i+1]):
print "swapping "+str(array[i])+" with "+str(array[i+1])
print "The array is "+str(array)
array[i], array[i+1] = array[i+1], array[i]
print "***********"
print "The sorted array is "+str(array)
#Main program
bubble_sort([8,5,6,7,9,1,0])