Bubble Sort
Table of Contents
Bubble Sort
The largest element “bubbles up” to its correct position in each pass.
Time Complexity: O(n^2).
Concept: Repeatedly steps through the list, comparing adjacent elements and swapping them if they are in the wrong order.
Steps
- Compare adjacent elements and swap them if they are out of order.
- Repeat the process until no swaps are made in a pass.
Example
Let’s sort the array [5, 2, 4, 6, 1, 3] using bubble sort.
Step-by-Step Walkthrough:
-
Initial array: [5, 2, 4, 6, 1, 3]
-
Pass 1 (i = 0):
- Compare 5, 2: Since 5 > 2, swap. Array: [2, 5, 4, 6, 1, 3]
- Compare 5, 4: Since 5 > 4, swap. Array: [2, 4, 5, 6, 1, 3]
- Compare 5, 6: No swap (5 < 6). Array: [2, 4, 5, 6, 1, 3]
- Compare 6, 1: Since 6 > 1, swap. Array: [2, 4, 5, 1, 6, 3]
- Compare 6, 3: Since 6 > 3, swap. Array: [2, 4, 5, 1, 3, 6]
- Largest element 6 is now at the end.
-
Pass 2 (i = 1):
- Compare 2, 4: No swap (2 < 4). Array: [2, 4, 5, 1, 3, 6]
- Compare 4, 5: No swap (4 < 5). Array: [2, 4, 5, 1, 3, 6]
- Compare 5, 1: Since 5 > 1, swap. Array: [2, 4, 1, 5, 3, 6]
- Compare 5, 3: Since 5 > 3, swap. Array: [2, 4, 1, 3, 5, 6]
- Second largest element 5 is now in place.
-
Pass 3 (i = 2):
- Compare 2, 4: No swap (2 < 4). Array: [2, 4, 1, 3, 5, 6]
- Compare 4, 1: Since 4 > 1, swap. Array: [2, 1, 4, 3, 5, 6]
- Compare 4, 3: Since 4 > 3, swap. Array: [2, 1, 3, 4, 5, 6]
- Third largest element 4 is now in place.
-
Pass 4 (i = 3):
- Compare 2, 1: Since 2 > 1, swap. Array: [1, 2, 3, 4, 5, 6]
- Compare 2, 3: No swap (2 < 3). Array: [1, 2, 3, 4, 5, 6]
- Array is now sorted, but the algorithm continues to check.
-
Pass 5 (i = 4):
- Compare 1, 2: No swap (1 < 2). Array: [1, 2, 3, 4, 5, 6]
- No swaps needed; array is sorted.
-
Final Sorted Array: [1, 2, 3, 4, 5, 6]
Explanation
- A is the input array.
- Outer loop i tracks the number of passes, ensuring the largest unsorted element moves to the end in each pass.
- Inner loop j compares adjacent elements and swaps them if A[j] > A[j + 1].
- Continue until no swaps are needed or the array is fully sorted.
Why is the upper limit of the inner loop n - i - 2 ?
- Because, with every iteration of i, the element at the last will be the greatest number in the array.
- With the first iteration of i, the greatest element will be at the last index.
- With the second iteration of i, the second greatest element will be at the second from the last index.
- …
Complexity analysis
- Time Complexity: O(n²) in worst and average cases, O(n) in best case (already sorted array with no swaps).
- Space Complexity: O(1) (in-place sorting).
- Use Case: Simple to implement but inefficient for large datasets; best for small arrays or educational purposes.
- Optimization: Can be improved by adding a flag to check if any swaps occurred in a pass. If no swaps, the array is sorted, and the algorithm can terminate early.
- For comparison with insertion sort, bubble sort is generally less efficient because it performs more swaps and doesn’t adapt as well to partially sorted arrays.