Skip to content

Commit 695ac54

Browse files
authored
Update jumpSearch.js
1 parent e92c4ae commit 695ac54

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

Search/jumpSearch.js

+21-23
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,29 @@
55
* The advantage against binary search is that Jump Search traversed back only once.
66
*/
77

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;
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+
lowerBound = step;
14+
step += step;
15+
if (lowerBound >= length) {
16+
return -1;
17+
}
1818
}
19-
}
20-
21-
const upperBound = Math.min(step, length);
22-
while (arr[lowerBound] < value)
23-
{
24-
lowerBound++;
25-
if (lowerBound === upperBound){
26-
return -1;
19+
20+
const upperBound = Math.min(step, length);
21+
while (arr[lowerBound] < value) {
22+
lowerBound++;
23+
if (lowerBound === upperBound) {
24+
return -1;
25+
}
26+
}
27+
if (arr[lowerBound] === value) {
28+
return lowerBound;
2729
}
28-
}
29-
if (arr[lowerBound] === value){
30-
return lowerBound;
31-
}
32-
return -1;
30+
return -1;
3331
}
3432
const arr = [0,0,4,7,10,23,34,40,55,68,77,90]
3533
jumpSearch(arr,4);

0 commit comments

Comments
 (0)