Searching
Searching
Linear, binary, ternary, jump, exponential search and quickselect.
Linear Search
▶ viz
Scan elements one by one until the target is found or the input is exhausted.
O(n) · O(1) space
Binary Search
▶ viz
Find a target in a sorted array by repeatedly halving the search range.
O(log n) · O(1) space
Ternary Search
▶ viz
Find the extremum of a unimodal function by discarding one third of the range per step.
O(log n) · O(1) space
Jump Search
▶ viz
Search a sorted array by jumping ahead in fixed blocks of size √n, then scanning linearly within the block.
O(√n) · O(1) space
Exponential Search
▶ viz
Double an index until the target is bracketed, then binary search inside that bracket.
O(log n) · O(1) space
Quickselect
▶ viz
Find the k-th smallest element in expected O(n) by partitioning like quicksort but recursing into only one side.
O(n²) · O(1) space