Python Program for Bubble Sort

Write a Python Program to Sort List items using Bubble sort using for loop, while loop, and functions with a practical example.

Python Program for Bubble Sort using For Loop

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

TIP: Please refer to the List article to understand everything about them in Python.

a = []
number = int(input("Please Enter the Total Elements : "))
for i in range(number):
    value = int(input("Please enter the %d Item : " %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 Result 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. Next, We are using the If statement inside the Python program loop 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 the 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 is the same as the above. However, we replaced the for loop with While Loop to sort list elements using 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 : " %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 List in Ascending Order : ", a)
Please Enter the Total Number of Elements : 5
Please enter the 0 Element : 5
Please enter the 1 Element : -3
Please enter the 2 Element : 10
Please enter the 3 Element : 5
Please enter the 4 Element : 7
The 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.

def bubFunc(a, val):
    for i in range(val -1):
        for j in range(val - i - 1):
            if(a[j] > a[j + 1]):
                 temp = a[j]
                 a[j] = a[j + 1]
                 a[j + 1] = temp

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

bubFunc(a, val)
print("The List in Ascending Order : ", a)
Please Enter the Total Elements : 4
Please enter the 0 Element : 2
Please enter the 1 Element : 1
Please enter the 2 Element : -1
Please enter the 3 Element : 0
The List in Ascending Order :  [-1, 0, 1, 2]