Sentinel technique
Introduction / Definition
A sentinel is a special marker used to simplify termination or boundary checks. The algorithm stops when the sentinel is encountered. It is useful in searching and input processing. The sentinel must be distinguishable from valid data.
Syntax / Structure / Formula
while item != SENTINEL: process(item)
Detailed Explanation
The marker represents the end of meaningful data or a special stopping condition. Correct use requires that it cannot be mistaken for ordinary data.
Example
Example: A search can place the target temporarily at the end as a sentinel.
Output / Result
The loop can terminate when the target is encountered.
Important Points / Notes
- Sentinel must be distinguishable.
- Handle empty inputs correctly.
Mini Practice Question
What property must a sentinel have?