Skip to content

Commit 7dcedbf

Browse files
committed
fix(searches): jump_search returns -1 for empty list (was IndexError)
Closes #15085. Empty collection cannot contain the target; add an early return instead of indexing arr[-1]. Doctest added + verified.
1 parent 8e0817e commit 7dcedbf

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

searches/jump_search.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ def jump_search[T: Comparable](arr: Sequence[T], item: T) -> int:
3333
10
3434
>>> jump_search(["aa", "bb", "cc", "dd", "ee", "ff"], "ee")
3535
4
36+
>>> jump_search([], 5)
37+
-1
3638
"""
3739

3840
arr_size = len(arr)
41+
if arr_size == 0:
42+
return -1
43+
3944
block_size = int(math.sqrt(arr_size))
4045

4146
prev = 0

0 commit comments

Comments
 (0)