Java Notes
Complete guide to sorting algorithms — Bubble, Selection, Insertion, Merge, Quick, and Heap sort with Java code, complexity analysis, and visual step-throughs.
Comparison of Sorting Algorithms
| Algorithm | Best | Average | Worst | Space | Stable | In-place |
|---|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | ✅ | ✅ |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | ❌ | ✅ |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | ✅ | ✅ |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | ✅ | ❌ |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | ❌ | ✅ |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | ❌ | ✅ |
Merge Sort ⭐
Divide array in half, sort each half, merge sorted halves.
Time: O(n log n) always | Space: O(n) | Stable: Yes
Quick Sort ⭐
Pick a pivot, partition array so elements < pivot are left, > pivot are right.
| Partition | [3, 1, 0, 2] [7] [8, 10] |
| Recurse | [0, 1, 2, 3] [7] [8, 10] |
| Result | [0, 1, 2, 3, 7, 8, 10] |
Time: O(n log n) avg, O(n²) worst | Space: O(log n) | Stable: No
When to Use Which
| Scenario | Best Algorithm | Why |
|---|---|---|
| Small array (n < 50) | Insertion Sort | Low overhead, good cache performance |
| General purpose | Merge Sort | Guaranteed O(n log n), stable |
| In-place needed | Quick Sort | O(1) extra space (O(log n) stack) |
| Nearly sorted data | Insertion Sort | O(n) for nearly sorted |
| Stability required | Merge Sort | Preserves equal element order |
| Memory constrained | Heap Sort | O(1) space, O(n log n) guaranteed |
Interview Questions
Q1: Why is QuickSort generally faster than MergeSort in practice?
Answer: QuickSort has better cache locality (works on contiguous memory), lower constant factors, and requires less memory (in-place). MergeSort has guaranteed O(n log n) but needs O(n) extra space and has more memory allocation overhead.
Q2: What does "stable" mean in sorting?
Answer: A stable sort preserves the relative order of elements with equal keys. If you sort students by grade and two have grade 'A', a stable sort keeps them in their original order. Merge Sort and Insertion Sort are stable; QuickSort and Heap Sort are not.
Q3: What is the worst case for QuickSort and how to avoid it?
Answer: Worst case O(n²) occurs when pivot is always the smallest/largest element (already sorted array with last-element pivot). Avoid with: random pivot selection, median-of-three, or IntroSort (switches to HeapSort when recursion is too deep).
Q4: How does Java's Arrays.sort() work?
Answer: For primitives: Dual-Pivot Quicksort (two pivots, three partitions). For objects: TimSort (hybrid merge sort + insertion sort, stable). TimSort exploits existing order in data (runs).
Q5: Can you sort in O(n) time?
Answer: Yes, for restricted inputs: Counting Sort (integers in known range), Radix Sort (fixed-length numbers/strings), Bucket Sort (uniformly distributed data). These are non-comparison sorts and break the O(n log n) comparison-based lower bound.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Sorting Algorithms in Java.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, dsa, sorting, sorting algorithms in java
Related Java Master Course Topics