Table of contents
1.
Introduction
2.
What is Bubble Sort in Python?
3.
How Bubble Sort Works
4.
Bubble Sort Algorithm
4.1.
Pseudocode
4.2.
Explanation
5.
Implementing Bubble Sort in Python
5.1.
Basic Implementation
5.2.
Python
5.3.
Explanation
5.4.
Optimized Version with Early Exit
5.5.
Python
5.6.
Explanation
5.7.
Time Complexity
5.8.
Space Complexity
6.
Advantages of Bubble Sort Algorithm in Python
7.
Disadvantages of the Bubble Sort Algorithm in Python
8.
Applications of Bubble Sort
9.
Frequently Asked Questions 
9.1.
Is Bubble Sort stable?
9.2.
Can Bubble Sort be used for large datasets?
9.3.
What is the difference between Bubble Sort and Selection Sort?
9.4.
How can I optimize Bubble Sort?
10.
Conclusion
Last Updated: Aug 20, 2025
Medium

Bubble Sort in Python

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Bubble Sort is one of the simplest sorting algorithms in computer science. It works by repeatedly swapping adjacent elements if they are in the wrong order. This process continues until the list is sorted. 

Bubble Sort in Python

Despite its simplicity, Bubble Sort is not very efficient for large datasets, but it is a great algorithm for beginners to learn and understand the basics of sorting.

What is Bubble Sort in Python?

Bubble Sort in Python is a simple comparison-based sorting method. It repeatedly scans through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until no swaps are needed, meaning the list is sorted. Although easy to understand, it is less efficient for large datasets

How Bubble Sort Works

The Bubble Sort algorithm compares each pair of adjacent elements in a list. If the elements are out of order, they are swapped. This process repeats until no more swaps are needed, indicating that the list is sorted. Here's a step-by-step explanation:

  1. Start from the beginning of the list.
     
  2. Compare the first and second elements.
     
  3. If the first element is greater than the second, swap them.
     
  4. Move to the next pair of elements and repeat the comparison and swapping if necessary.
     
  5. Continue this process until the end of the list.
     
  6. Repeat the entire process for the remaining elements, excluding the last sorted elements, until no swaps are needed.

Bubble Sort Algorithm

Pseudocode

function bubbleSort(arr):
    n = length of arr
    for i from 0 to n-1:
        for j from 0 to n-i-1:
            if arr[j] > arr[j+1]:
                swap arr[j] and arr[j+1]

Explanation

  • The outer loop runs from the beginning to the end of the list.
  • The inner loop compares each pair of adjacent elements and swaps them if they are in the wrong order.
  • With each iteration of the outer loop, the largest unsorted element is placed in its correct position at the end of the list.
  • The inner loop reduces its range with each iteration, as the largest elements are already sorted.

Implementing Bubble Sort in Python

Basic Implementation

  • Python

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]

# Example usage
arr = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(arr)
print("Sorted array:", arr)
You can also try this code with Online Python Compiler
Run Code


Output

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

Explanation

  • The function bubble_sort takes a list arr as input.
     
  • The outer loop runs n times, where n is the length of the list.
     
  • The inner loop runs from the beginning of the list to n-i-1, comparing and swapping adjacent elements if necessary.
     
  • The result is a sorted list.

Optimized Version with Early Exit

  • Python

Python

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
arr = [64, 34, 25, 12, 22, 11, 90]
bubble_sort_optimized(arr)
print("Sorted array:", arr)
You can also try this code with Online Python Compiler
Run Code


Output

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

Explanation

  • The optimized version includes a flag swapped to track if any elements were swapped during an iteration.
     
  • If no elements were swapped, the list is already sorted, and the function can exit early, reducing unnecessary iterations.

Time Complexity

  • Best Case: O(n)O(n)O(n) - When the list is already sorted.
     
  • Average Case: O(n2)O(n^2)O(n2) - For a random list.
     
  • Worst Case: O(n2)O(n^2)O(n2) - When the list is sorted in reverse order.

Space Complexity

  • Space Complexity: O(1)O(1)O(1) - Bubble Sort is an in-place sorting algorithm, meaning it requires a constant amount of additional space.

Advantages of Bubble Sort Algorithm in Python

  • Simple and easy to understand.
     
  • Does not require additional memory.
     
  • Useful for small datasets or nearly sorted lists.

Disadvantages of the Bubble Sort Algorithm in Python

  • Inefficient for large datasets.
     
  • High time complexity compared to other sorting algorithms, such as QuickSort or MergeSort.

Applications of Bubble Sort

  • Educational Purposes: Great for teaching the basics of sorting algorithms.
     
  • Small Datasets: Can be used for sorting small lists.
     
  • Nearly Sorted Data: Efficient for data that is almost sorted.

Frequently Asked Questions 

Is Bubble Sort stable?

Yes, Bubble Sort is a stable sorting algorithm, meaning it preserves the relative order of equal elements.

Can Bubble Sort be used for large datasets?

While it can be used, it is not efficient for large datasets due to its high time complexity.

What is the difference between Bubble Sort and Selection Sort?

Bubble Sort repeatedly swaps adjacent elements if they are in the wrong order, while Selection Sort selects the minimum element from the unsorted part and swaps it with the first unsorted element.

How can I optimize Bubble Sort?

By using a flag to track if any elements were swapped during an iteration, you can exit the loop early if the list is already sorted.

Conclusion

In conclusion, Bubble Sort remains a fundamental algorithm in computer science, offering beginners a simple way to understand sorting concepts. While not efficient for large datasets, its clarity and ease of implementation in Python make it a valuable starting point for learning nested loops and basic algorithmic logic.

Recommended Readings:

 

Live masterclass