Bubble Sort Program in Python

In this article, you will learn and get code on bubble sort in Python. But before going through the program on bubble sort, if you're not aware about logic used behind it, refer to Bubble Sort Logic and Example to understand about the topic.

Bubble Sort using List

This program sorts a list using bubble sort technique. The question is, write a program in Python to apply bubble sort technique to sort a list. The answer to this question is given below:

nums = []
print("Enter the Size of List: ")
tot = int(input())

print("Enter " + str(tot) + " Numbers: ")
for i in range(tot):
    nums.insert(i, int(input()))

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

print("The Sorted List is:")
for i in range(tot):
    print(nums[i])

Here is its sample run:

bubble sort python

Now supply the size of list say 6 and then enter any six numbers say 60, 10, 50, 20, 40, 30, press ENTER key to sort the list using bubble sort technique as shown in the sample run given below:

bubble sort program in python

Note - The str() method is used to convert an int type value to string type.

Note - The range() returns a sequence of values. The value starts with 0 and increments by 1, by default. It stops before a number specified as its argument. For example, the following block of code:

for i in range(3):
    print(i)

prints 0 1 2 on output. The total execution of print(i) statement is exactly three times (the value given as argument of range()). Each number gets printed in new line. Therefore from above program, the following statement:

for i in range(tot-1):

is used to executed the following block of code:

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

(tot-i-1) number of times. In similar way the second for loop (the inner one) is used.

Since in bubble sort, each adjacent element gets compared and swapped, if required. Therefore with this block of code, we've applied the bubble sort technique to sort the given list of numbers.

Modified Version of Previous Program

This is the modified version of previous program. The only thing added in this program is end, to output the thing in Python. Since print() outputs the thing present inside its braces terminated with an automatic newline. Therefore we've used end to end the thing with exactly given in its braces.

nums = []
print(end="Enter the Size of List: ")
tot = int(input())

print(end="Enter " + str(tot) + " Numbers: ")
for i in range(tot):
    nums.insert(i, int(input()))

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

print(end="\nThe Sorted List is: ")
for i in range(tot):
    print(end=str(nums[i]) + " ")
print()

Here is its sample run with user input 5 as size, and 5, 4, 3, 2, 1 as five numbers:

bubble sort using list python

Python Online Test


« Previous Program Next Program »