Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Linear search
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===With a sentinel=== The basic algorithm above makes two comparisons per iteration: one to check if {{math|''L''<sub>''i''</sub>}} equals ''T'', and the other to check if {{math|''i''}} still points to a valid index of the list. By adding an extra record {{math|''L''<sub>''n''</sub>}} to the list (a [[sentinel value]]) that equals the target, the second comparison can be eliminated until the end of the search, making the algorithm faster. The search will reach the sentinel if the target is not contained within the list.{{Sfn|Knuth|1998|loc=Β§6.1 ("Sequential search"), subsection "Algorithm Q"}} # Set {{math|''i''}} to 0. # If {{math|''L''<sub>''i''</sub> {{=}} ''T''}}, go to step 4. # Increase {{math|''i''}} by 1 and go to step 2. # If {{math|''i'' < ''n''}}, the search terminates successfully; return {{math|''i''}}. Else, the search terminates unsuccessfully.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)