Algorithms / 7 visualizations
Searching Algorithm Visualizers
Linear, jump and binary search, rotated arrays and first/last position: why sorted data changes everything.
Open the lab → Every one runs on your own input, step by step, with the code highlighted in Java, Python, C++, C or JavaScript. Free, no sign-up.
All searching visualizations
- Linear SearchO(n)
Check every value from left to right until you find the target or run out of values.
- Jump SearchO(√n)
On a sorted array, jump ahead √n values at a time until you pass the target, then walk back through that one block with a linear scan.
- Binary SearchO(log n)
On a sorted array, look at the middle value. If it is too small, throw away the left half. If too big, throw away the right half.
- Search in a Rotated Sorted ArrayO(log n)
Cut at the middle. One of the two halves is always fully sorted, and you can tell which by comparing its ends. If the target lies inside that sorted half, search there; otherwise search the other half.
- First and Last Position of a ValueO(log n)
Run binary search twice. When you hit the target, do not stop: remember the index, then keep going left to find the first copy, or right to find the last.
- Search a Sorted 2D MatrixO(r + c)
Start at the top-right corner. Everything left of it in its row is smaller and everything below it in its column is bigger. Too big: step left and drop that column. Too small: step down and drop that row.
- Median of Two Sorted ArraysO(log min(m, n))
Cut the shorter array at i and the longer at j so the two left parts hold half of all values. The cut is right when every left value ≤ every right value: A[i−1] ≤ B[j] and B[j−1] ≤ A[i]. Binary-search i until that holds; the median sits at the cut.