Kumar-laxmi / Algorithms

A Repository for algorithms in C, C++, Python and Java
Apache License 2.0
326 stars 367 forks source link

Adding the Sentinel linear Search #1719

Closed Aarsh30 closed 8 months ago

Aarsh30 commented 1 year ago

Sentinel Linear Search is a variation of the Linear Search algorithm that incorporates a sentinel value to optimize the search process. In traditional linear search, we iterate through each element of an array sequentially until we find the desired value or reach the end of the array. In Sentinel Linear Search, we add a sentinel value, which is the target value we are searching for, at the end of the array. By doing this, we eliminate the need for an additional comparison within the loop to check if we have reached the end of the array

🛠️ Issue (#1521)

Issue no ##1521

✔️ Check List (Check all the applicable boxes)

📄 Algorithm

Here is the step-by-step process of the Sentinel Linear Search algorithm:

Initialize the target value to be searched. Store the last element of the array in a variable, and replace it with the target value. Set up a loop to iterate through the array from the beginning. Within the loop, check if the current element is equal to the target value. If it is, the target value has been found at the last position (the sentinel position). Return the index of the target value. If it is not, continue iterating through the array. If the loop completes without finding the target value, it means the target value is not present in the array. Return a value indicating the absence of the target value.

📷 Screenshots

c c++ java python

github-actions[bot] commented 1 year ago

Thanks for your contribution we will soon check your PR @Aarsh30 :)

github-actions[bot] commented 8 months ago

Stale pull request message