Introduction
Bubble Sort is one of the simplest sorting algorithms.
The idea is:
- repeatedly compare adjacent elements
- swap them if they are in wrong order
- largest elements gradually move to the end
This movement looks like bubbles rising upward, hence the name:
Bubble SortThis problem helps in understanding:
- sorting basics
- swapping
- nested loops
- comparison-based sorting
Example
Input:arr = [5, 1, 4, 2, 8]
Output:
[1, 2, 4, 5, 8]
Explanation: Adjacent elements are compared and swapped repeatedly until array becomes sorted.
Constraints
1 <= arr.length <= 10^5-10^4 <= arr[i] <= 10^4Approach 1 : Basic Bubble Sort
Explanation
The simplest way to sort using Bubble Sort is:
- Traverse array repeatedly
- Compare adjacent elements
- Swap if left element is greater
- Continue until array becomes sorted
After every pass:
- largest unsorted element
moves to correct position
Steps
- Traverse array.
- Compare adjacent elements.
- Swap if elements are unordered.
- Repeat for all passes.
- Return sorted array.
Dry Run
Input:[5, 1, 4, 2, 8]
Pass 1:
5 > 1
Swap:
[1, 5, 4, 2, 8]
5 > 4
Swap:
[1, 4, 5, 2, 8]
5 > 2
Swap: [1, 4, 2, 5, 8]
5 < 8
No swap
Largest element fixed:
8
Continue remaining passes...
Final Result:
[1, 2, 4, 5, 8]
Basic Bubble Sort Code
Complexity Analysis
Time Complexity: O(n²)Explanation:
Nested loops compare all pairs.
Space Complexity: O(1)
Explanation:
Sorting is performed in-place.
Approach 2 : Optimized Bubble Sort
Explanation
The optimized version improves performance by checking:
Was any swap performed?If no swap occurs:
- array is already sorted
- traversal stops early
This reduces unnecessary passes.
Steps
- Traverse array.
- Track swap occurrence.
- Swap unordered elements.
- If no swaps occur:
- stop traversal early
- Return sorted array.
Dry Run
Input:[1, 2, 3, 4]
Pass 1: 1 < 2
No swap
2 < 3 No swap
3 < 4 No swap
No swaps occurred Array already sorted
Final Result:
[1, 2, 3, 4]
Optimized Bubble Sort Code
Complexity Analysis
Time Complexity: O(n)Explanation: Already sorted array requires only one pass.
Average Case Time Complexity: O(n²)
Worst Case Time Complexity: O(n²)
Space Complexity: O(1)
Explanation:
Sorting is performed in-place.
Edge Cases
- Empty array
- Single element array
- Already sorted array
- Reverse sorted array
- Duplicate elements present
Why This Problem is Important
Bubble Sort helps in understanding:
- Sorting fundamentals
- Swapping logic
- Nested loop traversal
- Comparison-based sorting
- In-place sorting
It is one of the most basic sorting algorithms taught in DSA.
Real-World Applications
Bubble Sort concepts are used in:
- Educational visualization
- Small datasets
- Embedded systems
- Sorting demonstrations
- Algorithm teaching tools
Common Mistakes
- Incorrect loop bounds
- Missing swap logic
- Wrong comparison conditions
- Forgetting optimization flag
Interview Tips
Interviewers often expect:
- Sorting fundamentals
- Time complexity explanation
- Optimization understanding
Always explain why:
- largest element moves to end
after every pass
Related Questions
- Selection Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- Sort 0s 1s 2s
Final Takeaway
Bubble Sort is a fundamental sorting algorithm that teaches comparison-based sorting and swapping techniques. Understanding Bubble Sort builds a strong foundation for advanced sorting algorithms and interview problems.