Bubble sort program in python. Firstly, we must aware that what is Bubble sort ? Bubble Sort is the sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. This python project is useful for beginners and CBSE KV School Class 11 and Class 12 students computer science practical file and NIELIT O Level Programming and Problem Solving through Python ( Module M3-R5),Computer science B SC and B Tech.
Objective of the program
Write a function to input a list and arrange the list in ascending order using Bubble sort. This python program with source code is discussed in this post.
The source code screenshot and the source code is given at the last of column in this post
Explanation of source code
l=eval(input(“Enter the list to arrange”)) # first a list is inputted
for i in range(len(l)-1): # first for loop i=0 to len(l)-1
for j in range(len(l)-1): #nested for loop from loop i=0 to len(l)-1
if l[j]>l[j+1]: #comparision between first and the next number if condition is true
l[j],l[j+1]=l[j+1],l[j] #if first one is true then this are swipped
print(“Arranged list :”,l) #The list is printed in asending using bubble sort
Output of bubble sort program in python
Testing of the program
Here we input the list [9,8,7,6,3,4] and the arranged list [3,4,6,7,8,9].
Test again and this time input is [6,3,9,8,5,6,1,1] and arranged ascending list is [1,1,3,5,6,6,8,9]
Our Bubble Sort program in python language is working fine.
Source code-> click here
for more python programs -> click here