Skip to content

Commit 0288779

Browse files
fix: Add comment for sentinel restore and fix main block
1 parent 0770595 commit 0288779

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

searches/sentinel_linear_search.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Sentinel Linear Search Algorithm
3-
A variation of linear search that reduces comparisons.
3+
A variation of linear search that reduces comparisons per iteration.
44
Reference: https://en.wikipedia.org/wiki/Linear_search#With_a_sentinel
55
"""
66

@@ -27,6 +27,7 @@ def sentinel_linear_search(arr: list[int], target: int) -> int:
2727
while arr[i] != target:
2828
i += 1
2929

30+
# Restore the original last element to prevent mutation of the input array
3031
arr[n - 1] = last
3132

3233
if i < n - 1 or arr[n - 1] == target:
@@ -36,5 +37,5 @@ def sentinel_linear_search(arr: list[int], target: int) -> int:
3637

3738
if __name__ == "__main__":
3839
import doctest
39-
4040
doctest.testmod()
41+
print(sentinel_linear_search([1, 2, 3, 4, 5], 3))

0 commit comments

Comments
 (0)