Monday, 12 June 2017
Friday, 3 February 2017
Bubble Sort Program in Python
In bubble sorting first and second elements are compared if first one is greater than second one then both are exchanged, here below 25 is greater than 15 so their position is exchanged.
Then the second and third elements are compared but not like above case here 25 is not greater than 36 then nothing will happen here. and so on.
First Iteration
Bubble Sort Program in Python:
print 'Bubble Sorting'
print 'Enter the element of the list and write 'done/Done' to stop'
loop = True
list = []
while loop == True:
lis = raw_input()
if lis == 'done' or lis=='Done':
loop =False
else:
list.append(int(lis))
leng = len(list)
print leng
for i in range(0,leng):
for j in range(0,leng):
if j < leng-1:
if list[j] > list[j+1]:
a = list[j]
list[j] = list[j+1]
list[j+1]=a
print 'Sorted list will be',list
Subscribe to:
Posts (Atom)