Master Python Bubble Sort Example: A Step-by-Step Guide

Logan Jul 13, 2026

Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. This algorithm is named because smaller elements "bubble" to the top of the list.

an image of a computer screen with text on it and the words, method 1 - bubble sort in python
an image of a computer screen with text on it and the words, method 1 - bubble sort in python

In this Python example, we will sort a list of numbers using the bubble sort algorithm. Let's delve into the code and understand the logic behind this algorithm.

an image of a screen shot of a web page with the text bubble sort in python
an image of a screen shot of a web page with the text bubble sort in python

Basic Bubble Sort Algorithm

The basic bubble sort algorithm works by repeatedly swapping the adjacent elements if they are in the wrong order until the entire list is sorted. Here's a simple implementation in Python:

an image of a computer screen with the words example and numbers in red, blue, yellow
an image of a computer screen with the words example and numbers in red, blue, yellow

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

Basic Implementation Analysis

bubble sort with python in front of a chalkboard and an arrow pointing to it
bubble sort with python in front of a chalkboard and an arrow pointing to it

The outer loop runs for the total number of passes, and the inner loop goes through each element in the list. If the current element is greater than the next one, they are swapped, allowing the smaller element to "bubble" to the top after each pass.

However, there's a potential efficiency improvement we can make to this algorithm.

Optimizing the Bubble Sort

the text bubble sort in python on a black background with green and red font that reads,
the text bubble sort in python on a black background with green and red font that reads,

Since the largest element "bubbles" to the end of the list on the first pass, we can improve our algorithm to stop early if no more swaps are needed.

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

The Bubble Sort in Action

Coding For Beginners Python - Sorting Algorithms
Coding For Beginners Python - Sorting Algorithms

Now that we have understood the algorithm let's see how it works with an example. Suppose we have the following list of numbers:

  • 3
  • 7
  • 8
  • 1
  • 5
  • 2
  • 6
  • 4
Bubble Sort Algorithm with Python using List Example
Bubble Sort Algorithm with Python using List Example
Bubble Sort – Algorithm in Java, C++, Python with Example Code
Bubble Sort – Algorithm in Java, C++, Python with Example Code
a black background with green text and an image of a computer screen that says,'quick sort in python bubble sort in python
a black background with green text and an image of a computer screen that says,'quick sort in python bubble sort in python
Quick Sort Python Code, Python Script Troubleshooting, Python Reverse Sorting Function, Python List Reverse Sort, Python Sorting Algorithm Tutorial, Python Selection Sort Code, Python Code Sorting Numbers, Python Sorting Methods, Multiprocessing Python Examples
Quick Sort Python Code, Python Script Troubleshooting, Python Reverse Sorting Function, Python List Reverse Sort, Python Sorting Algorithm Tutorial, Python Selection Sort Code, Python Code Sorting Numbers, Python Sorting Methods, Multiprocessing Python Examples
an image of a programming program with the text python list sorting code
an image of a programming program with the text python list sorting code
Insertion Sort in Python
Insertion Sort in Python
Sort Algorithms
Sort Algorithms
Sorting in Python Quick Sort Python Code, Python Reverse Sorting Function, Python List Reverse Sort, Python List Sorting Code, Python Sorting Algorithm Tutorial, Python Selection Sort Code, Python Code Sorting Numbers, Python Sorting Methods, Python List Sorting Program
Sorting in Python Quick Sort Python Code, Python Reverse Sorting Function, Python List Reverse Sort, Python List Sorting Code, Python Sorting Algorithm Tutorial, Python Selection Sort Code, Python Code Sorting Numbers, Python Sorting Methods, Python List Sorting Program
a screen shot of a web page with the text insertion sort using python
a screen shot of a web page with the text insertion sort using python
Client Challenge
Client Challenge
the bubble sort worksheet is shown in this file, and it contains instructions to make
the bubble sort worksheet is shown in this file, and it contains instructions to make
Python Tuple Sort | How to Sort Python Tuples? | Sorting Tuples ⋆ IpCisco
Python Tuple Sort | How to Sort Python Tuples? | Sorting Tuples ⋆ IpCisco
Bubble Sort
Bubble Sort
Students Keen on Learning Python and Ruby - Despite JavaScript Demand
Students Keen on Learning Python and Ruby - Despite JavaScript Demand
Bubble Sort With JavaScript
Bubble Sort With JavaScript
Bubble Sort algorithm in C#
Bubble Sort algorithm in C#
bubble sort doodle
bubble sort doodle
Python List Sorting!!
Python List Sorting!!
an image of a computer screen with the text robofied list sortr sorted in python
an image of a computer screen with the text robofied list sortr sorted in python
Python Lists in Python Programming
Python Lists in Python Programming

After the first pass:

  • 1
  • 7
  • 8
  • 3
  • 5
  • 2
  • 6
  • 4

After the second pass:

  • 1
  • 3
  • 8
  • 7
  • 5
  • 2
  • 6
  • 4

And so on, until the list is fully sorted.

Bubble Sort Versus Other Sorting Algorithms

Though bubble sort is simple to understand and implement, it's not the most efficient sorting algorithm. Its worst-case and average complexity is O(n^2), where n is the number of items being sorted, making it inefficient on large lists.

Algorithms like quicksort or mergesort are generally preferred for large datasets, offering O(n log n) average complexity. However, bubble sort still has merits in certain situations, such as when the dataset is nearly sorted or small.

In terms of Python libraries, there's no built-in bubble sort algorithm like there is for other algorithms such as sort or sorted. Therefore, understanding how to implement bubble sort can provide valuable insights into how sorting algorithms work.

With this understanding, you're now equipped to use and even improve bubble sort in your Python projects. Happy coding!