Add SentinelLinearSearch.java and Update README.md #355
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Explaination:
Sentinel linear search is an addition to the usual linear search algorithm. Here, the issue of not finding the key (to find) element is removed. We store the last element of the array in a separate variable and put the key in its place.
While the finding index (i) iterates through the array, there are two possible conditions:
After the interations are completed, the stored last element is put back in its place.
If either the index of the iterative variable (i.e., the value of "I") is smaller than the length of the array-1 or the last element's value (the original value that we put back in place) is equal to the key value, the element is considered to be found and the index is returned
Else, the element is considered to be not found in the array and the index -1 is returned.
The README.md has been updated accordingly