All Patterns

Sorting Algorithms

Understand how the classic comparison and non-comparison sorts work, and when each one is the right tool.

When to use

  • Implementing merge, quick, counting, or radix sort
  • Quickselect for the k-th element
  • Reasoning about stability and complexity
  • Choosing a sort for the input shape

When NOT to use

  • A library sort already suffices
  • The problem is really about what you do after sorting

Common traps

  • Quicksort worst case on sorted input or bad pivots
  • Losing stability when you reorder equal keys
  • Off-by-one in merge boundaries

Key Invariant

Comparison sorts bottom out at O(n log n); counting and radix beat it only when the key range is bounded

Problems (5)

#201Counting Sort
easy
#199Merge Sort
medium
#200Quick Sort
medium
#202Radix Sort
medium
#275Find Kth Largest Element using Quickselect
medium