Skip to content

Commit 9120191

Browse files
committed
Add jump search algorithm implementation with tests and docs
1 parent 3f810dc commit 9120191

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

.vscode/launch.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"type": "node",
6+
"request": "launch",
7+
"name": "Debug Current TS File",
8+
"runtimeExecutable": "tsx",
9+
"skipFiles": ["<node_internals>/**"],
10+
"args": ["${file}"],
11+
"cwd": "${workspaceFolder}",
12+
"internalConsoleOptions": "openOnSessionStart",
13+
"resolveSourceMapLocations": [
14+
"${workspaceFolder}/**",
15+
"!**/node_modules/**"
16+
]
17+
}
18+
]
19+
}

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ Linear search is a searching algorithm that sequentially checks each element in
77
- **Best Case:** **O(1)** (Element found at the beginning)
88
- **Worst Case:** **O(n)** (Element at the end or not present)
99

10+
# Jump Search
11+
12+
Jump Search is a searching searching algorithm that works on **sorted** arrays. It reduces the number of comparisons by jumping ahead in fixed steps instead of checking elements one by one.
13+
14+
## Time Complexity
15+
16+
- **Best Case:** O(1) (First element is the target)
17+
- **Worst Case:** O(sqrt{n})
18+
1019
# Binary Search
1120

1221
Binary search is a searching algorithm that find the desiired element in a **sorted** sequence by repeatedly dividing the search interval in half until the target is found or the interval is empty.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export const jumpSearch = <T>(
2+
sortedArray: T[],
3+
target: T,
4+
jumpSize?: number
5+
): { index: number; element: T } | null => {
6+
const length = sortedArray.length
7+
8+
if (typeof jumpSize === 'undefined')
9+
jumpSize = Math.floor(Math.sqrt(length))
10+
else {
11+
if (typeof jumpSize !== 'number')
12+
throw new TypeError('jumpSize must be a number')
13+
else if (!Number.isInteger(jumpSize))
14+
throw new Error('jumpSize must be an integer')
15+
else if (jumpSize <= 0)
16+
throw new RangeError('jumpSize must be a positive integer')
17+
else if (jumpSize >= length)
18+
throw new RangeError(
19+
'jumpSize must be smaller than the array length'
20+
)
21+
}
22+
23+
for (let index = 0; index < length; ) {
24+
const element = sortedArray[index]
25+
26+
if (target === element) return { index, element }
27+
if (target > element) {
28+
if (index + jumpSize > length - 1) {
29+
for (let j = index + 1; j < length; j++) {
30+
const element = sortedArray[j]
31+
if (target === sortedArray[j]) return { index: j, element }
32+
}
33+
return null
34+
}
35+
index += jumpSize
36+
continue
37+
}
38+
if (target < element) {
39+
for (let j = index; j > index - jumpSize; j--) {
40+
const element = sortedArray[j]
41+
if (target === sortedArray[j]) return { index: j, element }
42+
}
43+
return null
44+
}
45+
}
46+
47+
return null
48+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { jumpSearch } from '../../../src/algorithms/searching/jumpSearch'
3+
4+
describe('jumpSearch', () => {
5+
describe('when array is empty', () => {
6+
it('should return null', () => {
7+
expect(jumpSearch([], 1)).toBeNull()
8+
})
9+
})
10+
11+
describe('when using default jump size', () => {
12+
describe('and the target is in the list', () => {
13+
const vectors = Array.from(
14+
{ length: 10 },
15+
(_, index) => index + 1
16+
).map((int, index) => ({
17+
array: Array.from({ length: 10 }, (_, index) => index + 1),
18+
target: int,
19+
expected: { index, element: int }
20+
}))
21+
22+
it('should retur the target with its index', () => {
23+
vectors.forEach(({ array, target, expected }) => {
24+
expect(jumpSearch(array, target)).toStrictEqual(expected)
25+
})
26+
})
27+
})
28+
describe('and the target is not in the list', () => {
29+
const vectors = Array.from(
30+
{ length: 10 },
31+
(_, index) => index + 1
32+
).map(() => ({
33+
array: Array.from({ length: 10 }, (_, index) => index + 1),
34+
target: 100
35+
}))
36+
37+
it('should retur the target with its index', () => {
38+
vectors.forEach(({ array, target }) => {
39+
expect(jumpSearch(array, target)).toBeNull()
40+
})
41+
})
42+
})
43+
})
44+
})

0 commit comments

Comments
 (0)