8000 fix: handle the case when target is last in `jumpSearch` (#231) · Algorithms-Learn/TypeScript@b3a3bd7 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3a3bd7

Browse files
authored
fix: handle the case when target is last in jumpSearch (TheAlgorithms#231)
1 parent 23ba61b commit b3a3bd7

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

search/jump_search.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export const jumpSearch = (array: number[], target: number): number => {
2525
let currentIdx: number = 0,
2626
stepSize: number = Math.floor(Math.sqrt(array.length)),
2727
nextIdx: number = stepSize;
28-
28+
2929
while (array[nextIdx - 1] < target) {
3030
currentIdx = nextIdx;
3131
nextIdx += stepSize;
3232

33-
if (nextIdx >= array.length) {
34-
nextIdx = array.length - 1;
33+
if (nextIdx > array.length) {
34+
nextIdx = array.length;
3535
break;
3636
}
3737
}

search/test/jump_search.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ describe("Jump search", () => {
55
[[], 1, -1],
66
[[1, 2, 3, 4, 5], 4, 3],
77
[[1, 3, 5, 8, 9], 4, -1],
8+
[[1, 3, 5, 8], 8, 3],
9+
[[1, 3, 5, 8], 9, -1],
10+
[[1, 3, 5, 8], 7, -1],
11+
[[1, 3, 5, 8, 10], 10, 4],
12+
[[1, 3, 5, 8, 10], 11, -1],
13+
[[1, 3, 5, 8, 10], 9, -1],
14+
[[5], 5, 0],
15+
[[5], 100, -1],
16+
[[], 100, -1],
817
])(
918
"of %o , searching for %o, expected %i",
1019
(array: any[], target: any, index: number) => {

0 commit comments

Comments
 (0)
0