Skip to content

Commit 85f734d

Browse files
committed
Improve search efficiency with early termination
check
1 parent a85b7da commit 85f734d

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

searches/double_linear_search.py

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def double_linear_search(array: list[int], search_item: int) -> int:
2121
"""
2222
# define the start and end index of the given array
2323
start_ind, end_ind = 0, len(array) - 1
24+
25+
# Early termination check
26+
if search_item < array[start_ind] or search_item > array[end_ind]:
27+
return -1
28+
2429
while start_ind <= end_ind:
2530
if array[start_ind] == search_item:
2631
return start_ind
@@ -29,6 +34,7 @@ def double_linear_search(array: list[int], search_item: int) -> int:
2934
else:
3035
start_ind += 1
3136
end_ind -= 1
37+
3238
# returns -1 if search_item is not found in array
3339
return -1
3440

0 commit comments

Comments
 (0)