Visualizing Bubble Sort: A Step-by-Step Guide
Introduction to Bubble Sort
Bubble Sort is one of the simplest sorting algorithms. It 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 no swaps are needed, which indicates that the list is sorted. While not the most efficient, its simplicity makes it a great starting point for learning sorting algorithms. Think of it like bubbles rising to the top – larger elements 'bubble' to the end with each pass. This is a fundamental topic for data structures and algorithms - DSA. Refer to our Beginner Sheet for a quick start.
How Bubble Sort Works: A Visual Guide
Let's visualize how Bubble Sort sorts an array: [5, 1, 4, 2, 8]
- Pass 1:
- ( 5 1 4 2 8 ) --> ( 1 5 4 2 8 ), Swap since 5 > 1
- ( 1 5 4 2 8 ) --> ( 1 4 5 2 8 ), Swap since 5 > 4
- ( 1 4 5 2 8 ) --> ( 1 4 2 5 8 ), Swap since 5 > 2
- ( 1 4 2 5 8 ) --> ( 1 4 2 5 8 ), No swap since 5 < 8
At the end of the first pass, the largest element, 8, is in its correct position.
- Pass 2:
- ( 1 4 2 5 8 ) --> ( 1 4 2 5 8 ), No swap since 1 < 4
- ( 1 4 2 5 8 ) --> ( 1 2 4 5 8 ), Swap since 4 > 2
- ( 1 2 4 5 8 ) --> ( 1 2 4 5 8 ), No swap since 4 < 5
Now, 5 is also in its correct position.
- Pass 3:
- ( 1 2 4 5 8 ) --> ( 1 2 4 5 8 ), No swap since 1 < 2
- ( 1 2 4 5 8 ) --> ( 1 2 4 5 8 ), No swap since 2 < 4
Now, 4 is also in its correct position.
- Pass 4:
- ( 1 2 4 5 8 ) --> ( 1 2 4 5 8 ), No swap since 1 < 2
The array is now sorted: [1, 2, 4, 5, 8]
Bubble Sort Code Example (Python)
def bubble_sort(arr):
n = len(arr)
for i in range(n):
# Flag to optimize - if no swaps occur in a pass, the array is sorted
swapped = False
for j in range(0, n - i - 1):
# Compare adjacent elements
if arr[j] > arr[j + 1]:
# Swap if the element found is greater than the next element
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
# If no two elements were swapped in inner loop, the array is sorted
if swapped == False:
break
return arr
# Example usage:
arr = [5, 1, 4, 2, 8]
sorted_arr = bubble_sort(arr)
print("Sorted array:", sorted_arr) # Output: Sorted array: [1, 2, 4, 5, 8]
Complexity Analysis
- Time Complexity: O(n^2) in the worst and average case, O(n) in the best case (when the array is already sorted).
- Space Complexity: O(1), as it sorts in place.
When to Use Bubble Sort
Bubble sort is generally not suitable for large datasets due to its quadratic time complexity. It's best used for:
- Small datasets
- Educational purposes – easy to understand and implement
- Nearly sorted data (with an optimized implementation that uses a flag to check for swaps)
Further Learning
Interested in mastering other sorting algorithms or improving your coding skills? SWE180 offers comprehensive resources, including Core Subject Study Guides, Mock Interviews, and Resume Reviews. Consider exploring our roadmap for structured learning. Prepare for your aptitude tests and practice with our flashcards. Unlock your potential with expert mentorship.