Sentinel Linear Search

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 2

Input: arr[] = {10, 20, 180, 30, 60, 50, 110, 100, 70}, x = 90
Output: Not found

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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] =
    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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

180 is present at index 2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.