Sentinal Linear Search as the name suggests is a type of Linear Search where the number of comparisons are reduced as compared to a traditional linear search. When a linear search is performed on an array of size N then in the worst case a total of N comparisons are made when the element to be searched is compared to all the elements of the array and (N + 1) comparisons are made for the index of the element to be compared so that the index is not out of bounds of the array which can be reduced in a Sentinel Linear Search.
In this search, the last element of the array is replaced with the element to be searched and then the linear search is performed on the array without checking whether the current index is inside the index range of the array or not because the element to be searched will definitely be found inside the array even if it was not present in the original array since the last element got replaced with it. So, the index to be checked will never be out of bounds of the array. The number of comparisons in the worst case here will be (N + 2).
Examples:
Input: arr[] = {10, 20, 180, 30, 60, 50, 110, 100, 70}, x = 180
Output: 180 is present at index 2Input: arr[] = {10, 20, 180, 30, 60, 50, 110, 100, 70}, x = 90
Output: Not found
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to search x in the given array void sentinelSearch(int arr[], int n, int x) { // Last element of the array int last = arr[n - 1]; // Element to be searched is // placed at the last index arr[n - 1] = x; int i = 0; while (arr[i] != x) i++; // Put the last element back arr[n - 1] = last; if ((i < n - 1) || (x == arr[n - 1])) cout << x << " is present at index " << i; else cout << "Not found"; } // Driver code int main() { int arr[] = { 10, 20, 180, 30, 60, 50, 110, 100, 70 }; int n = sizeof(arr) / sizeof(arr[0]); int x = 180; sentinelSearch(arr, n, x); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to search x in the given array static void sentinelSearch(int arr[], int n, int x) { // Last element of the array int last = arr[n - 1]; // Element to be searched is // placed at the last index arr[n - 1] = x; int i = 0; while (arr[i] != x) i++; // Put the last element back arr[n - 1] = last; if ((i < n - 1) || (x == arr[n - 1])) System.out.println(x + " is present at index " + i); else System.out.println("Not found"); } // Driver code public static void main (String[] args) { int arr[] = { 10, 20, 180, 30, 60, 50, 110, 100, 70 }; int n = arr.length; int x = 180; sentinelSearch(arr, n, x); } } // This code is contributed by Ankit Rai |
Python3
# Python3 implementation of the approach # Function to search x in the given array def sentinelSearch(arr, n, x) : # Last element of the array last = arr[n - 1] # Element to be searched is # placed at the last index arr[n - 1] = x i = 0 while (arr[i] != x) : i += 1 # Put the last element back arr[n - 1] = last if ((i < n - 1) or (x == arr[n - 1])) : print(x, "is present at index", i) else : print("Not found") # Driver code arr = [ 10, 20, 180, 30, 60, 50, 110, 100, 70 ] n = len(arr) x = 180 sentinelSearch(arr, n, x) # This code is contributed by # divyamohan123 |
C#
// C# implementation of the approach using System; class GFG { // Function to search x in the given array static void sentinelSearch(int [] arr, int n, int x) { // Last element of the array int last = arr[n - 1]; // Element to be searched is // placed at the last index arr[n - 1] = x; int i = 0; while (arr[i] != x) i++; // Put the last element back arr[n - 1] = last; if ((i < n - 1) || (x == arr[n - 1])) Console.WriteLine(x + " is present" + " at index " + i); else Console.WriteLine("Not found"); } // Driver code public static void Main () { int [] arr = { 10, 20, 180, 30, 60, 50, 110, 100, 70 }; int n = arr.Length; int x = 180; sentinelSearch(arr, n, x); } } // This code is contributed by Mohit kumar |
180 is present at index 2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Linear Search vs Binary Search
- Linear Search
- C/C++ Program for Linear Search
- Java Program for Linear Search
- Linear search using Multi-threading
- Number of comparisons in each direction for m queries in linear search
- Interpolation search vs Binary search
- Anagram Substring Search (Or Search for all permutations)
- Why is Binary Search preferred over Ternary Search?
- Sublist Search (Search a linked list in another list)
- Best First Search (Informed Search)
- Repeatedly search an element by doubling it after every successful search
- Meta Binary Search | One-Sided Binary Search
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- K'th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Find Two Missing Numbers | Set 1 (An Interesting Linear Time Solution)
- Sorted subsequence of size 3 in linear time using constant space
- Median of two sorted arrays of different sizes | Set 1 (Linear)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

