Jump Search
Example in TypeScript
function jumpSearch(arr: number[], target: number): number | null {
const n = arr.length;
let step = Math.floor(Math.sqrt(n));
let prev = 0;
while (arr[Math.min(step, n) - 1] < target) {
prev = step;
step += Math.floor(Math.sqrt(n));
if (prev >= n) {
return null;
}
}
while (arr[prev] < target) {
prev++;
if (prev === Math.min(step, n)) {
return null;
}
}
if (arr[prev] === target) {
return prev;
}
return null;
}Usage
Last updated