package com.thealgorithms.searches; import com.thealgorithms.devutils.searches.SearchAlgorithm; /** * Sentinel Linear Search is a variation of linear search that eliminates the * need to check the array bounds in each iteration by placing the search key * at the end of the array as a sentinel value. * *
* The algorithm works by: * 1. Storing the last element of the array * 2. Placing the search key at the last position (sentinel) * 3. Searching from the beginning without bound checking * 4. If found before the last position, return the index * 5. If found at the last position, check if it was originally there * *
* Time Complexity: * - Best case: O(1) - when the element is at the first position * - Average case: O(n) - when the element is in the middle * - Worst case: O(n) - when the element is not present * *
* Space Complexity: O(1) - only uses constant extra space * *
* Advantages over regular linear search:
* - Reduces the number of comparisons by eliminating bound checking
* - Slightly more efficient in practice due to fewer conditional checks
*
* @author TheAlgorithms Contributors
* @see LinearSearch
* @see SearchAlgorithm
*/
public class SentinelLinearSearch implements SearchAlgorithm {
/**
* Performs sentinel linear search on the given array.
*
* @param array the array to search in
* @param key the element to search for
* @param