Bubble Sort Program in Python

As a developer, while working with a dataset, I needed to sort a list of numbers without relying on Python’s built-in sort() function. The task reminded me of one of the first algorithms I learned long ago.

Bubble Sort is simple, easy to understand, and a great way to build a strong foundation in algorithms. Although it’s not the fastest sorting method, it’s often the first algorithm I teach beginners because it demonstrates how comparisons and swaps work step by step.

In this tutorial, I’ll walk you through how Bubble Sort works in Python. I’ll share multiple methods and provide complete code examples, so you can practice and adapt them to your projects.

What is Bubble Sort?

Bubble Sort is a comparison-based algorithm. It works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order.

This process continues until the list is sorted. Think of it like bubbles rising to the surface of water; the largest numbers “bubble up” to the end of the list with each pass.

Method 1 – Basic Bubble Sort in Python

Let’s start with the simplest version of Bubble Sort.

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]

# Example usage
numbers = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(numbers)
print("Sorted list:", numbers)

Output:

Sorted list: [11, 12, 22, 25, 34, 64, 90]

I executed the above example code and added the screenshot below.

bubble sort python code

Here, I loop through the list multiple times. Each pass makes sure the largest element moves to the end.

Method 2 – Optimized Bubble Sort

In real projects, efficiency matters. So, I often use an optimized version of Bubble Sort that stops early if the list is already sorted.

def bubble_sort_optimized(arr):
    n = len(arr)
    for i in range(n):
        swapped = False
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
                swapped = True
        if not swapped:
            break

# Example usage
numbers = [5, 1, 4, 2, 8]
bubble_sort_optimized(numbers)
print("Sorted list:", numbers)

Output:

Sorted list: [1, 2, 4, 5, 8]

I executed the above example code and added the screenshot below.

python program for bubble sort

Notice how this version saves time when the input list is already sorted or nearly sorted.

Method 3 – Bubble Sort in Descending Order

Sometimes, I need the list sorted in reverse order. With just a small change in the comparison, Bubble Sort can handle that too.

def bubble_sort_desc(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] < arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]

# Example usage
scores = [78, 95, 62, 88, 70]
bubble_sort_desc(scores)
print("Sorted list (descending):", scores)

Output:

Sorted list (descending): [95, 88, 78, 70, 62]

I executed the above example code and added the screenshot below.

bubble sort python

This is useful when ranking test scores or sales numbers where the highest values matter most.

Method 4 – Bubble Sort with User Input

Sometimes, I want to give users the flexibility to enter their numbers. Here’s how I do that in Python:

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]

# Taking input from user
user_input = input("Enter numbers separated by spaces: ")
numbers = list(map(int, user_input.split()))

bubble_sort(numbers)
print("Sorted list:", numbers)

If I enter:

45 12 78 34 23

The program outputs:

Sorted list: [12, 23, 34, 45, 78]

This example shows how Bubble Sort can be combined with user input to make the program interactive. Users can enter any numbers, and the algorithm neatly sorts them in ascending order.

When Should You Use Bubble Sort?

From my experience, Bubble Sort is best for learning and small datasets. It’s not efficient for large lists because its time complexity is O(n²).

For real-world applications, I usually go with faster algorithms like QuickSort, MergeSort, or Python’s built-in sort() method. But when teaching or debugging, Bubble Sort is still my go-to example.

Conclusion

Most of the time, I use Bubble Sort to explain the basics of sorting to beginners. It’s simple, visual, and helps people understand how comparisons and swaps work step by step.

Now that you’ve seen multiple ways to implement Bubble Sort in Python, try experimenting with your datasets. Whether it’s sorting grocery prices, test scores, or work reports, you’ll quickly see how the algorithm behaves.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.