-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathJumpSearchOptimized.js
More file actions
59 lines (49 loc) · 1.46 KB
/
JumpSearchOptimized.js
File metadata and controls
59 lines (49 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* This version of Jump Search works for both ascending and descending sorted arrays.
*
* Time Complexity: O(√n)
* Space Complexity: O(1)
*
* Example:
* jumpSearchOptimized([1, 3, 5, 7, 9, 11], 7) -> 3
* jumpSearchOptimized([20, 15, 10, 5, 0], 10) -> 2
*/
function jumpSearchOptimized(arr, target) {
if (!Array.isArray(arr) || arr.length === 0) return -1
const n = arr.length
const step = Math.floor(Math.sqrt(n))
let prev = 0
// Detect array order
const isAscending = arr[0] < arr[n - 1]
// Jump in blocks based on order
while (prev < n) {
const next = Math.min(prev + step, n)
const value = arr[next - 1]
if ((isAscending && value >= target) || (!isAscending && value <= target)) {
// Linear search in the found block
for (let i = prev; i < next; i++) {
if (arr[i] === target) return i
}
return -1
}
prev = next
}
return -1
}
module.exports = { jumpSearchOptimized }
/* -----------------------------------------
Quick local test: run `node Search/JumpSearchOptimized.js`
----------------------------------------- */
if (require.main === module) {
const tests = [
{ arr: [1, 3, 5, 7, 9, 11], target: 7 },
{ arr: [20, 15, 10, 5, 0], target: 10 },
{ arr: [2, 4, 6, 8, 10, 12], target: 11 },
{ arr: [], target: 3 }
]
tests.forEach(({ arr, target }) => {
console.log(
`Array: [${arr}] | Target: ${target} | Index: ${jumpSearchOptimized(arr, target)}`
)
})
}