Search and sort
Search finds one value. Sort puts every value in order so a later search can discard half the range. These articles cover recursion, binary search and its boundaries, bubble sort, merge sort, and quicksort, including how to choose among them and the off-by-one errors a half-open range invites.
What Is Recursion? Smaller Copies and a Base Case
Recursion solves a smaller copy of the same problem and combines what comes back. One call, the base case, has to stop without calling again, or the call stack grows until it overflows.
What Is Binary Search? Throw Away Half of a Sorted Array
Binary search compares the target with the middle of a sorted array and throws away half. Keep an invariant for where the target can still sit, and return -1 when that range is empty.
Why Is Bubble Sort O(n²)? Neighbour Swaps Until It Settles
Bubble sort swaps neighbours until a pass makes no swaps. It is easy to get right and quadratic, so it is a contrast, not the sort to reach for.
How Does Merge Sort Stay O(n log n) in Every Case?
Merge sort splits the array, sorts the halves, and merges two sorted runs with two pointers. That is O(n log n) time and O(n) extra memory in every case you implement here.
How Does Quicksort Partition Around a Pivot? (Optional Enrichment)
Optional enrichment: quicksort partitions values around a pivot, then recursively sorts the two sides. Balanced partitions give O(n log n) time, while repeated extreme pivots give O(n²).
What Is an Off-by-One Error? Ranges, Remainders, and Indexes
An off-by-one error is a boundary that is one step away from the index you meant. Empty ranges, a zero remainder, and a match you fail to exclude are the usual places it shows up.
What Is a Sorted Run? The Merge Step Between Two Halves
A sorted run is a slice that is already in order. Merge sort's merge step takes two runs and writes one, using a pointer on each.
What Changes When Recursion Makes Two Calls?
Two recursive calls create a branching call tree. Correct indexing establishes the base values; overlapping branches explain exponential recomputation.
How Does Binary Search Find the First or Last Equal Index?
Boundary binary search records a match and continues on one side. Inclusive bounds must move past mid so the candidate range strictly shrinks.
How Do You Choose Between Checking, Bubble Sort, and Merge Sort?
Checking order is linear, bubble sort is quadratic, and merge sort is linearithmic. The task contract determines which operation is needed.