Skip to content

Commit e92c4ae

Browse files
committed
Added JumpSearch algorithm
1 parent 46eca98 commit e92c4ae

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Search/jumpSearch.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* The Jump Search algorithm allows to combine a linear search with a speed optimization.
2+
* This means that instead of going 1 by 1, we will increase the step of √n and increase that
3+
* step of √n which make the step getting bigger and bigger.
4+
* The asymptotic analysis of Jump Search is o(√n). Like the binary search, it needs to be sorted.
5+
* The advantage against binary search is that Jump Search traversed back only once.
6+
*/
7+
8+
const jumpSearch = (arr, value)=>{
9+
const length = arr.length;
10+
let step = Math.floor(Math.sqrt(length));
11+
let lowerBound = 0;
12+
while (arr[Math.min(step, length)-1] < value)
13+
{
14+
lowerBound = step;
15+
step += step;
16+
if (lowerBound >= length){
17+
return -1;
18+
}
19+
}
20+
21+
const upperBound = Math.min(step, length);
22+
while (arr[lowerBound] < value)
23+
{
24+
lowerBound++;
25+
if (lowerBound === upperBound){
26+
return -1;
27+
}
28+
}
29+
if (arr[lowerBound] === value){
30+
return lowerBound;
31+
}
32+
return -1;
33+
}
34+
const arr = [0,0,4,7,10,23,34,40,55,68,77,90]
35+
jumpSearch(arr,4);
36+
jumpSearch(arr,34);
37+
jumpSearch(arr,77);

0 commit comments

Comments
 (0)