Python Program for Bubble Sort

Write a Python Program to Sort List items using Bubble sort with a practical example.

Python Program for Bubble Sort using For Loop

This Python program allows the user to enter the List Size. Next, we are using For Loop to insert elements into the List. After that, we are sorting the list items using the Python bubble sort algorithm.

TIP: Please refer to List article to understand everything about Python Lists.

# Python program for Bubble Sort

a = []
number = int(input("Please Enter the Total Number of Elements : "))
for i in range(number):
    value = int(input("Please enter the %d Element of List1 : " %i))
    a.append(value)

for i in range(number -1):
    for j in range(number - i - 1):
        if(a[j] > a[j + 1]):
             temp = a[j]
             a[j] = a[j + 1]
             a[j + 1] = temp

print("The Sorted List in Ascending Order : ", a)
Python program for Bubble Sort 1

In this Python program, we are using Nested For Loop to iterate each element in a given List. Inside the loop, we are using the If statement to sort items in an ascending order using Bubble Sort.

First For Loop – First Iteration: for o in range (3)
The condition is True. So, it enters into second for loop

Second For Loop – First Iteration: for o in range(4 – 0 – 1). The condition inside the For Loop is True. So, it enters into the If Statement

if(a[0] > a[1]) => if(10 > -4) – It means the condition is True
temp = 10
a[j] = a[j + 1] => a[0] = a[1] => a[0] = -4
a[j + 1] = temp => a[1] = 10

Now the List =  -4 10 10 5.

Second For Loop – Second Iteration: for 1 in range(4 – 0 – 1) – True
if(10 > 10) – It means the condition is False. Do the same for the remaining Iterations.

Python Program for Bubble Sort using While Loop

This bubble sort is the same as the above. However, we replaced the for loop with While Loop to sort list elements using bubble sort.

# Python program for Bubble Sort

a = []
number = int(input("Please Enter the Total Number of Elements : "))
for i in range(number):
    value = int(input("Please enter the %d Element of List1 : " %i))
    a.append(value)

i = 0
while(i < number -1):
    j = 0
    while(j < number - i - 1):
        if(a[j] > a[j + 1]):
             temp = a[j]
             a[j] = a[j + 1]
             a[j + 1] = temp
        j = j + 1
    i = i + 1

print("The Sorted List in Ascending Order : ", a)

Python bubble sort using while loop output

Please Enter the Total Number of Elements : 5
Please enter the 0 Element of List1 : 5
Please enter the 1 Element of List1 : -3
Please enter the 2 Element of List1 : 10
Please enter the 3 Element of List1 : 5
Please enter the 4 Element of List1 : 7
The Sorted List in Ascending Order :  [-3, 5, 5, 7, 10]

Python Program for Bubble Sort using Functions

This bubble sort program is the same as the first example. But, we separated the logic to sort list elements using Functions.

# Python program for Bubble Sort
def bubblesort(a, number):
    for i in range(number -1):
        for j in range(number - i - 1):
            if(a[j] > a[j + 1]):
                 temp = a[j]
                 a[j] = a[j + 1]
                 a[j + 1] = temp

a = []
number = int(input("Please Enter the Total Number of Elements : "))
for i in range(number):
    value = int(input("Please enter the %d Element of List1 : " %i))
    a.append(value)

bubblesort(a, number)
print("The Sorted List in Ascending Order : ", a)

Python bubble sort using functions output

Please Enter the Total Number of Elements : 4
Please enter the 0 Element of List1 : 2
Please enter the 1 Element of List1 : 1
Please enter the 2 Element of List1 : -1
Please enter the 3 Element of List1 : 0
The Sorted List in Ascending Order :  [-1, 0, 1, 2]