DSA Notes
A thorough introduction to sorting — what sorting is, why it matters, classification of sorting algorithms (comparison vs non-comparison, stable vs unstable, in-place vs extra space), lower bound proof for comparison sorts, and a roadmap through all major sorting algorithms.
What Is Sorting?
Sorting is the process of rearranging a collection of elements into a defined order — most commonly ascending or descending by value.
Sorting is one of the most studied problems in computer science, not just because sorted data is useful on its own, but because so many other algorithms depend on sorted input:
- Binary search requires sorted arrays
- Merge operations work on sorted sequences
- Duplicate detection becomes O(n) on sorted data
- Database joins use sorting internally
- Data compression exploits sorted/grouped data
- Computational geometry sorts points by coordinate
Understanding sorting algorithms deeply — their mechanics, their trade-offs, their best and worst cases — is foundational to algorithm design.
The Sorting Lower Bound — Why O(n log n) is Optimal
Theorem: Any comparison-based sorting algorithm requires Ω(n log n) comparisons in the worst case.
Proof (decision tree argument):
Any comparison sort can be modeled as a binary decision tree:
- Each internal node represents a comparison (is arr[i] ≤ arr[j]?)
- Each leaf represents one specific sorted ordering
- For n elements, there are n! possible orderings → n! leaves minimum
- A binary tree with n! leaves has height ≥ log₂(n!) (ceiling)
- By Stirling's approximation: log₂(n!) ≈ n log₂(n) - n log₂(e) = Ω(n log n)
Conclusion: Any comparison sort needs Ω(n log n) comparisons. Merge Sort and Heap Sort achieve exactly O(n log n) → they are asymptotically optimal for comparison sorts.
Overview of All Sorting Algorithms
| Algorithm | Best | Average | Worst | Space | Stable | Type |
|---|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | ✅ | Comparison |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | ❌ | Comparison |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | ✅ | Comparison |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | ✅ | Comparison |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | ❌ | Comparison |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | ❌ | Comparison |
| Counting Sort | O(n+k) | O(n+k) | O(n+k) | O(k) | ✅ | Non-comp. |
| Radix Sort | O(nk) | O(nk) | O(nk) | O(n+k) | ✅ | Non-comp. |
Where k = range of values (Counting Sort), d = number of digits (Radix Sort)
Choosing the Right Sorting Algorithm
| └── YES | Insertion Sort (O(n) best case!) |
| └── YES | Insertion Sort (low overhead) |
| ├── YES | Merge Sort (always O(n log n), stable) |
| └── NO | Quick Sort (average O(n log n), best cache performance) |
| └── YES | Counting Sort if k is small, Radix Sort otherwise |
Real-World Sorting in Practice
Python's Built-in Sort — TimSort
TimSort is Python's sorting algorithm — a hybrid of Merge Sort and Insertion Sort. It runs in O(n) on nearly sorted data (using Insertion Sort on small runs) and O(n log n) otherwise. It is stable and has O(n) auxiliary space.
Java's Arrays.sort()
int[] arr = {3, 1, 4, 1, 5, 9, 2, 6};
Arrays.sort(arr); // Dual-pivot Quicksort for primitives
Integer[] arr2 = {3, 1, 4, 1};
Arrays.sort(arr2); // TimSort for objectsSummary
Sorting is one of the most fundamental operations in computing:
- Optimal comparison sort: O(n log n) — proven lower bound
- Best general algorithm: Quick Sort (average O(n log n), excellent cache)
- Guaranteed O(n log n): Merge Sort (+ stable), Heap Sort (+ O(1) space)
- Beats O(n log n): Counting Sort and Radix Sort for integer data
- Stable required: Merge Sort, Insertion Sort, TimSort
- Nearly sorted: Insertion Sort (O(n) best case)
In the following lessons, each sorting algorithm is covered in depth — its mechanism, implementation, analysis, and the specific scenarios where it excels.
*Next Lesson: Bubble Sort*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Sorting — Complete Introduction to Sorting Algorithms.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, sorting, introduction, sorting — complete introduction to sorting algorithms
Related Data Structures & Algorithms Topics