Searching Algorithms (linear search, binary search)
What is the optimal step size used in this jump search implementation?
function jumpSearch(arr, target) {
const step = Math.floor(Math.sqrt(arr.length));
let prev = 0;
while(arr[Math.min(step, arr.length) - 1] < target) {
prev = step;
step += Math.floor(Math.sqrt(arr.length));
if(prev >= arr.length) return -1;
}
while(arr[prev] < target) {
prev++;
if(prev === Math.min(step, arr.length)) return -1;
}
return arr[prev] === target ? prev : -1;
}