Bubble Sort in Python / Python Pragram

First we understand what is sorting  :

Ordering data in an increasing or decreasing manner is called sorting

Now we understand Bubble Sort :

Bubble sort is a simple sorting algorithm that repeatedly steps through a list , compares  adjacent  elements and swaps them if they are in the wrong order
The algorithm gets its name from the way smaller elements bubble to the top of the list during each pass

Input :

s=[ 6,4,9,1,7,2,3,8,5 ]

Output :

s=[ 1,2,3,4,5,6,7,8,9 ]

 

 

 

 

 

 

 

 

# Bubble sort 
s= [1,5,4,8,2,7,9,3,6]
for i in range(0,len(s)):
  for j in range(0,len(s)-1):    
    if s[j]>s[j+1] :        # we use  j in both side 
      c= s[j]        # we use swap method 
      s[j]=s[j+1]
      s[j+1] =c   # where is tem. variable 

print(s)


Output :
     s= [1,2,3,4,5,6,7,8,9]

 

For more practice program :

Python program to Identify Even and Odd Elements in List

Find the number is Armstrong number or not // python program

Find the Duplicate Elements in the List – Python List Program

How to Invert The x or y axis in Python ?

Leave a Comment

%d bloggers like this: