Skip to content

Commit dba31e3

Browse files
authored
feat: Add implementation of jump serach algorithm in typescript (TheAlgorithms#133)
* feat: add jump search * feat: add jump serach to directory.md * feat: jumpSearch test fix
1 parent 724d246 commit dba31e3

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
## Search
5151
* [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/binary_search.ts)
52+
* [Jump Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/jump_search.ts)
5253
* [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/linear_search.ts)
5354

5455
## Sorts

search/jump_search.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @function jumpSearch
3+
* @description Jump search algorithm for a sorted array.
4+
*
5+
* Jump search is a searching algorithm for sorted arrays that checks elements
6+
* by jumping ahead by fixed steps. The optimal step size is the square root of the array length.
7+
*
8+
* The algorithm works as follows:
9+
* 1.Start from the first element and jump by step size until finding an element that is greater than or equal to the target value.
10+
* 2.Go back one step and perform a linear search from there until finding the target value or reaching the end of the subarray.
11+
* 3.If the target value is found, return its index. Otherwise, return -1 to indicate that it is not in the array.
12+
*
13+
* @param {number[]} array - sorted list of numbers
14+
* @param {number} target - target number to search for
15+
* @return {number} - index of the target number in the list, or -1 if not found
16+
* @see [JumpSearch](https://www.geeksforgeeks.org/jump-search/)
17+
* @example jumpSearch([1,2,3], 2) => 1
18+
* @example jumpSearch([4,5,6], 2) => -1
19+
*/
20+
21+
export const jumpSearch = (array: number[], target: number): number => {
22+
if (array.length === 0) return -1;
23+
24+
// declare pointers for the current and next indexes and step size
25+
let currentIdx: number = 0,
26+
stepSize: number = Math.floor(Math.sqrt(array.length)),
27+
nextIdx: number = stepSize;
28+
29+
while (array[nextIdx - 1] < target) {
30+
currentIdx = nextIdx;
31+
nextIdx += stepSize;
32+
33+
if (nextIdx >= array.length) {
34+
nextIdx = array.length - 1;
35+
break;
36+
}
37+
}
38+
39+
for (let index = currentIdx; index < nextIdx; index++) {
40+
if(array[index] == target)
41+
{
42+
return index;
43+
}
44+
}
45+
46+
return -1;
47+
}

search/test/jump_search.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { jumpSearch } from "../jump_search";
2+
3+
describe("Jump search", () => {
4+
test.each([
5+
[[], 1, -1],
6+
[[1, 2, 3, 4, 5], 4, 3],
7+
[[1, 3, 5, 8, 9], 4, -1],
8+
])(
9+
"of %o , searching for %o, expected %i",
10+
(array: any[], target: any, index: number) => {
11+
expect(jumpSearch(array, target)).toStrictEqual(index)
12+
},
13+
);
14+
});

0 commit comments

Comments
 (0)