Skip to content

Commit a2d0251

Browse files
authored
Merge pull request vJechsmayr#75 from mu545/master
jump search implementation in js
2 parents 7c72f8f + 293aa24 commit a2d0251

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎SearchAlgorithms/jumpSearch.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function jumpSearch(arr, target) {
2+
let n = arr.length // size of array
3+
let m = Math.round(Math.sqrt(n)) // size of block to be jumped
4+
5+
var jumpIndex = 0
6+
7+
while (arr[jumpIndex]) {
8+
if (arr[jumpIndex] == target) {
9+
return 'found target at ' + String(jumpIndex) + ' position'
10+
} else {
11+
jumpIndex += m
12+
}
13+
}
14+
15+
for (var prevIndex = jumpIndex; prevIndex > 0; prevIndex--) {
16+
if (arr[prevIndex] == target) {
17+
return 'found target at ' + String(prevIndex) + ' position'
18+
} else if (arr[prevIndex] < target) {
19+
break
20+
}
21+
}
22+
23+
return 'not found'
24+
}

0 commit comments

Comments
 (0)