Search Visualizer
Searching
Compare direct scanning with range-halving search. Each step shows the active search space, eliminated indexes, and the synced code line.
O(log n)
Index 054
Index 167
Index 273
Index 391
Index 418
Index 529
Index 636
Index 742
Step Note
Use a rotated sorted copy and search the full range.
This visualizer displays a rotated sorted copy of the input values.
function searchRotated(array: number[], target: number) {
let left = 0
let right = array.length - 1
while (left <= right) {
const mid = Math.floor((left + right) / 2)
if (array[mid] === target) return mid
if (array[left] <= array[mid]) {
if (array[left] <= target && target < array[mid]) {
right = mid - 1
} else {
left = mid + 1
}
} else if (array[mid] < target && target <= array[right]) {
left = mid + 1
} else {
right = mid - 1
}
}
return -1
}