Bubble Sort in Python
Sorting means arranging elements in a particular order, such as smallest to largest or largest to smallest. Bubble Sort is one of the simplest sorting algorithms to learn because its working can be understood by comparing elements that are next to each other.
During each pass through the list, neighboring values are checked and exchanged whenever they appear in the wrong order. As the process continues, larger values gradually move toward the end of the list. This repeated movement gives the algorithm its name, Bubble Sort.
Although it is not suitable for efficiently handling very large datasets, Bubble Sort is useful for learning the basic ideas behind comparison-based sorting.
Table of Contents

YT:- DecodeIT
How Does Bubble Sort Work?
Bubble Sort repeatedly examines adjacent elements. If the element on the left is greater than the element immediately to its right, the two values are swapped.
After completing one complete pass, the largest value in the unsorted portion reaches its correct position. The next pass then works on the remaining unsorted elements.
Steps Involved in Bubble Sort
- Start from the first element of the list.
- Compare the current element with the next element.
- Swap the two values if they are arranged incorrectly.
- Continue this comparison until the end of the unsorted section is reached.
- Repeat the process for the remaining elements.
- Stop when the complete list is ordered.
Bubble Sort Algorithm Example
Consider the following list:
[5, 3, 8, 2]
During the first pass, neighboring values are compared from left to right. Whenever a larger value appears before a smaller one, they exchange positions. By the end of the pass, the largest value reaches the final position.
The same process is repeated for the remaining unsorted section until the complete sequence becomes:
[2, 3, 5, 8]
This illustrates the main idea behind Bubble Sort: repeated adjacent comparisons gradually place every element in its correct location.
Bubble Sort Program in Python
The following function implements Bubble Sort and includes an optimization that stops the algorithm when a complete pass makes no changes.
def bubble_sort(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
numbers = [64, 34, 25, 12, 22, 11, 90]
print("Original List:", numbers)
bubble_sort(numbers)
print("Sorted List:", numbers)
Output
Original List: [64, 34, 25, 12, 22, 11, 90]
Sorted List: [11, 12, 22, 25, 34, 64, 90]
Understanding the Python Code
The function takes the list as the arr argument, while n keeps track of how many elements the list contains.
The outer loop controls how many passes are performed. With every completed pass, one more element at the end of the unsorted section is placed correctly.
The inner loop compares neighboring positions. The condition:
if arr[j] > arr[j + 1]:
checks whether the left value should come after the right value. If that condition is true, Python swaps the two elements.
The swapped variable is initially set to False. Whenever an exchange happens, it becomes True. If an entire pass finishes without a swap, the algorithm knows that the list is already sorted and exits early.
Time Complexity of Bubble Sort
Time complexity describes how the amount of work performed by an algorithm changes as the input size grows. Bubble Sort can require many comparisons, particularly when the elements are arranged in an unfavorable order.
| Case | Time Complexity | Situation |
|---|---|---|
| Best Case | O(n) | The list is already sorted and the swap optimization detects it. |
| Average Case | O(n²) | The elements have a mixed arrangement. |
| Worst Case | O(n²) | The list is arranged in reverse order. |
The optimized version can finish in O(n) time when the input is already sorted. However, for average and worst-case inputs, Bubble Sort remains O(n²), which makes it inefficient for large collections.
Advantages of Bubble Sort
- The algorithm is straightforward to understand.
- Its implementation requires only a small amount of code.
- It is useful for learning comparison-based sorting.
- The optimized version can finish early when the list is already sorted.
- It sorts the values within the existing list without requiring a separate large data structure.
Limitations of Bubble Sort
- Its average and worst-case complexity is O(n²).
- It becomes slow as the number of elements increases.
- Other algorithms are generally more suitable for large datasets.
- It performs many comparisons and swaps compared with more advanced sorting techniques.
Bubble Sort vs Efficient Sorting Algorithms
Bubble Sort is mainly useful for educational purposes and small datasets. For larger collections, algorithms such as Merge Sort and Quick Sort generally provide better asymptotic performance, with typical O(n log n) behavior.
Python applications also commonly use the built-in sorting facilities instead of implementing Bubble Sort manually when performance and simplicity are the main goals.
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
Conclusion
Bubble Sort is a beginner-friendly sorting algorithm based on repeated comparisons between neighboring elements. Whenever two adjacent values are in the wrong order, they are exchanged, and repeated passes gradually arrange the complete list.
In Python, the algorithm can be implemented easily with nested loops. Adding a swapped flag improves the implementation by allowing it to stop as soon as a pass confirms that no further changes are required.
Although Bubble Sort is not an efficient choice for large datasets because of its O(n²) average and worst-case complexity, understanding it provides a strong foundation for learning more advanced sorting algorithms.
Frequently Asked Questions
What is Bubble Sort in Python?
Why is it called Bubble Sort?
The name comes from the way larger values gradually move toward the end of the list after repeated adjacent comparisons, similar to bubbles moving upward.
What is the time complexity of Bubble Sort?
The optimized version has a best-case complexity of O(n) when the list is already sorted. Its average and worst-case complexity is O(n²).
Can Bubble Sort be implemented using a for loop?
Yes. Nested for loops are commonly used to control the passes and adjacent comparisons.
Is Bubble Sort suitable for large datasets?
Generally, no. Its quadratic average and worst-case complexity can make it slow as the input size increases.
What is the main advantage of Bubble Sort?
Its biggest advantage is simplicity. The algorithm is easy to understand and is therefore useful when learning fundamental sorting concepts.
Keywords
Bubble Sort in Python, Bubble Sort Python example, Bubble Sort algorithm Python, Bubble Sort in PythonBubble Sort in PythonBubble Sort using for loop, Python Bubble Sort program, Bubble Sort implementation Python, Bubble Sort Python,Bubble Sort with example, Python sorting algorithms, Bubble Sort time complexity, Bubble Sort algorithm, sorting in Python, Python list sorting, comparison based sorting, Bubble Sort tutorial, Bubble Sort for beginners,Bubble Sort in Python, Bubble Sort Python