Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 28 additions & 46 deletions searches/sentinel_linear_search.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,40 @@
"""
This is pure Python implementation of sentinel linear search algorithm

For doctests run following command:
python -m doctest -v sentinel_linear_search.py
or
python3 -m doctest -v sentinel_linear_search.py

For manual testing run:
python sentinel_linear_search.py
Sentinel Linear Search Algorithm
A variation of linear search that reduces comparisons.
Reference: https://en.wikipedia.org/wiki/Linear_search#With_a_sentinel
"""


def sentinel_linear_search(sequence, target):
"""Pure implementation of sentinel linear search algorithm in Python

:param sequence: some sequence with comparable items
:param target: item value to search
:return: index of found item or None if item is not found

Examples:
>>> sentinel_linear_search([0, 5, 7, 10, 15], 0)
0

>>> sentinel_linear_search([0, 5, 7, 10, 15], 15)
4

>>> sentinel_linear_search([0, 5, 7, 10, 15], 5)
1

>>> sentinel_linear_search([0, 5, 7, 10, 15], 6)

def sentinel_linear_search(arr: list[int], target: int) -> int:
"""
sequence.append(target)
Search for target using sentinel method, return index or -1 if not found.

>>> sentinel_linear_search([1, 2, 3, 4, 5], 3)
2
>>> sentinel_linear_search([1, 2, 3, 4, 5], 6)
-1
>>> sentinel_linear_search([], 1)
-1
"""
n = len(arr)
if n == 0:
return -1

index = 0
while sequence[index] != target:
index += 1
last = arr[n - 1]
arr[n - 1] = target
i = 0

sequence.pop()
while arr[i] != target:
i += 1

if index == len(sequence):
return None
arr[n - 1] = last

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch restoring the original last element after the sentinel search - this prevents mutation of the input array. Worth adding a comment here explaining why the restore is neede,d since its not immediately obvious to readers why arr[n-1] = last exists.


return index
if i < n - 1 or arr[n - 1] == target:
return i
return -1


if __name__ == "__main__":
user_input = input("Enter numbers separated by comma:\n").strip()
sequence = [int(item) for item in user_input.split(",")]

target_input = input("Enter a single number to be found in the list:\n")
target = int(target_input)
result = sentinel_linear_search(sequence, target)
if result is not None:
print(f"{target} found at positions: {result}")
else:
print("Not found")
import doctest

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main block is missing a print statement after doctest.testmod()
to confirm tests passed. Small addition but consistent with the repo's
other implementations:

if __name__ == "__main__":
    import doctest
    doctest.testmod()
    print("All tests passed.")

doctest.testmod()