📜  前哨线性搜索

📅  最后修改于: 2021-05-04 22:10:01             🧑  作者: Mango

顾名思义,前哨线性搜索是一种线性搜索,与传统的线性搜索相比,它的比较次数有所减少。当对大小为N的数组执行线性搜索时,在最坏的情况下,当将要搜索的元素与数组的所有元素进行比较时,总共进行N次比较,并且对(N + 1)个进行比较要比较的元素的索引,以使索引不超出数组的范围,可以在Sentinel线性搜索中减少该范围。
在此搜索中,将数组的最后一个元素替换为要搜索的元素,然后对数组执行线性搜索,而无需检查当前索引是否在数组的索引范围内,因为要搜索的元素即使自从最后一个元素替换为原始数组以来,即使在原始数组中不存在该数组,也肯定会在数组内部找到它。因此,要检查的索引永远不会超出数组的范围。最坏情况下的比较次数为(N + 2)。
例子:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to search x in the given array
void sentinelSearch(int arr[], int n, int key)
{
 
    // Last element of the array
    int last = arr[n - 1];
 
    // Element to be searched is
    // placed at the last index
    arr[n - 1] = key;
    int i = 0;
 
    while (arr[i] != key)
        i++;
 
    // Put the last element back
    arr[n - 1] = last;
 
    if ((i < n - 1) || (arr[n - 1] == key))
        cout << key << " is present at index " << i;
    else
        cout << "Element 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 key = 180;
 
    sentinelSearch(arr, n, key);
 
    return 0;
}
// This code is contributed by Mandeep Dalavi


Java
// Java implementation of the approach
class GFG {
 
    // Function to search x in the given array
    static void sentinelSearch(int arr[], int n, int key)
    {
 
        // Last element of the array
        int last = arr[n - 1];
 
        // Element to be searched is
        // placed at the last index
        arr[n - 1] = key;
        int i = 0;
 
        while (arr[i] != key)
            i++;
 
        // Put the last element back
        arr[n - 1] = last;
 
        if ((i < n - 1) || (arr[n - 1] == key))
            System.out.println(key + " is present at index "
                               + i);
        else
            System.out.println("Element 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 key = 180;
 
        sentinelSearch(arr, n, key);
    }
}
 
// This code is contributed by Ankit Rai, Mandeep Dalavi


Python3
# Python3 implementation of the approach
# Function to search key in the given array
 
 
def sentinelSearch(arr, n, key):
 
    # Last element of the array
    last = arr[n - 1]
 
    # Element to be searched is
    # placed at the last index
    arr[n - 1] = key
    i = 0
 
    while (arr[i] != key):
        i += 1
 
    # Put the last element back
    arr[n - 1] = last
 
    if ((i < n - 1) or (arr[n - 1] == key)):
        print(key, "is present at index", i)
    else:
        print("Element Not found")
 
 
# Driver code
arr = [10, 20, 180, 30, 60, 50, 110, 100, 70]
n = len(arr)
key = 180
 
sentinelSearch(arr, n, key)
 
# This code is contributed by divyamohan123, Mandeep Dalavi


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 key)
    {
 
        // Last element of the array
        int last = arr[n - 1];
 
        // Element to be searched is
        // placed at the last index
        arr[n - 1] = key;
        int i = 0;
 
        while (arr[i] != key)
            i++;
 
        // Put the last element back
        arr[n - 1] = last;
 
        if ((i < n - 1) || (arr[n - 1] == key))
            Console.WriteLine(key + " is present"
                              + " at index " + i);
        else
            Console.WriteLine("Element Not found");
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr
            = { 10, 20, 180, 30, 60, 50, 110, 100, 70 };
        int n = arr.Length;
        int key = 180;
 
        sentinelSearch(arr, n, key);
    }
}
 
// This code is contributed by Mohit kumar, Mandeep Dalavi


输出:
180 is present at index 2