Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions JavaScript/jumpSearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function jumpSearch(arr, x, n) {
// Finding block size to be jumped
let step = Math.sqrt(n);

// Finding the block where element is present
let prev = 0;
while (arr[Math.min(step, n) - 1] < x) {
prev = step;
step += Math.sqrt(n);
if (prev >= n) {
return -1;
}
}

// Doing a linear search for x in
// block beginning with prev

while (arr[prev] < x) {
prev += 1;
if (prev == Math.min(step, n)) {
return -1;
}
}

if (arr[prev] == x) {
return prev;
}
return -1;
}

let arr = [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 ];
let x = 55;
let n = arr.length;

let index = jumpSearch(arr, x, n);
console.log(`Number ${x} is at index ${index}`);