Skip to content

Commit 8770024

Browse files
committed
feat(Search): add Jump Search optimized version (works for descending arrays) and add test cases
1 parent 08d8c6b commit 8770024

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

Search/JumpSearchOptimized.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* This version of Jump Search works for both ascending and descending sorted arrays.
3+
*
4+
* Time Complexity: O(√n)
5+
* Space Complexity: O(1)
6+
*
7+
* Example:
8+
* jumpSearchOptimized([1, 3, 5, 7, 9, 11], 7) -> 3
9+
* jumpSearchOptimized([20, 15, 10, 5, 0], 10) -> 2
10+
*/
11+
12+
function jumpSearchOptimized(arr, target) {
13+
if (!Array.isArray(arr) || arr.length === 0) return -1;
14+
15+
const n = arr.length;
16+
const step = Math.floor(Math.sqrt(n));
17+
let prev = 0;
18+
19+
// Detect array order
20+
const isAscending = arr[0] < arr[n - 1];
21+
22+
// Jump in blocks based on order
23+
while (prev < n) {
24+
const next = Math.min(prev + step, n);
25+
const value = arr[next - 1];
26+
27+
if ((isAscending && value >= target) || (!isAscending && value <= target)) {
28+
// Linear search in the found block
29+
for (let i = prev; i < next; i++) {
30+
if (arr[i] === target) return i;
31+
}
32+
return -1;
33+
}
34+
35+
prev = next;
36+
}
37+
38+
return -1;
39+
}
40+
41+
module.exports = { jumpSearchOptimized };
42+
43+
/* -----------------------------------------
44+
Quick local test: run `node Search/JumpSearchOptimized.js`
45+
----------------------------------------- */
46+
if (require.main === module) {
47+
const tests = [
48+
{ arr: [1, 3, 5, 7, 9, 11], target: 7 },
49+
{ arr: [20, 15, 10, 5, 0], target: 10 },
50+
{ arr: [2, 4, 6, 8, 10, 12], target: 11 },
51+
{ arr: [], target: 3 },
52+
];
53+
54+
tests.forEach(({ arr, target }) => {
55+
console.log(`Array: [${arr}] | Target: ${target} | Index: ${jumpSearchOptimized(arr, target)}`);
56+
});
57+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { jumpSearchOptimized } from '../JumpSearchOptimized'
2+
3+
test('jumpSearchOptimized([0, 0, 4, 7, 10, 23, 34, 40, 55, 68, 77, 90], 77) => 10', () => {
4+
const arr = [0, 0, 4, 7, 10, 23, 34, 40, 55, 68, 77, 90]
5+
const res = jumpSearchOptimized(arr, 77)
6+
expect(res).toEqual(10)
7+
})
8+
9+
test('jumpSearchOptimized([11, 12, 15, 65, 78, 90], 4) => -1', () => {
10+
const arr = [11, 12, 15, 65, 78, 90]
11+
const res = jumpSearchOptimized(arr, 4)
12+
expect(res).toEqual(-1)
13+
})
14+
15+
test('jumpSearchOptimized([11, 12, 15, 65, 78, 90], 11) => 0', () => {
16+
const arr = [11, 12, 15, 65, 78, 90]
17+
const res = jumpSearchOptimized(arr, 11)
18+
expect(res).toEqual(0)
19+
})
20+
21+
test('jumpSearchOptimized([], 50) => -1', () => {
22+
const arr = []
23+
const res = jumpSearchOptimized(arr, 50)
24+
expect(res).toEqual(-1)
25+
})
26+
27+
test('jumpSearchOptimized([5, 10, 15, 20, 25], 25) => 4', () => {
28+
const arr = [5, 10, 15, 20, 25]
29+
const res = jumpSearchOptimized(arr, 25)
30+
expect(res).toEqual(4)
31+
})
32+
33+
test('jumpSearchOptimized([1, 3, 5, 7, 9], 2) => -1', () => {
34+
const arr = [1, 3, 5, 7, 9]
35+
const res = jumpSearchOptimized(arr, 2)
36+
expect(res).toEqual(-1)
37+
})

0 commit comments

Comments
 (0)