Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb3a3bd7

Browse files
authored
fix: handle the case when target is last injumpSearch (#231)
1 parent23ba61b commitb3a3bd7

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
letcurrentIdx: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)

[8]ページ先頭

©2009-2025 Movatter.jp