Python program for bubble sort

Last updated 3 months, 1 week ago | 175 views 75     5

Tags:- Python

Python | Bubble sort program

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are not in the right order.

This is an example for bubble sort in python

# unordered list
l = [5,6,8,5,4,1,9,3,7]

# bubble sort approch for sorting
for i in range(len(l)-1,0,-1):
    for j in range(i):
        if l[j] > l[j+1]:
            l[j],l[j+1] = l[j+1],l[j]

print(l)
 

Output for the above code

>>> 
===================== RESTART: E:\py\bubble-sort.py =====================
[1, 3, 4, 5, 5, 6, 7, 8, 9]