Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SentinelLinearSearch.java and Update README.md #355

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Krounosity
Copy link

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:

  • The key element is found before the last index and the loop is broken, or
  • The index "i" hits the end of the array

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

@Krounosity Krounosity marked this pull request as ready for review October 12, 2024 04:11
@Krounosity
Copy link
Author

Time Complexity: O(n)
Space Complexity: O(1)

Copy link
Owner

@kelvins kelvins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please fix the README links?

@Krounosity
Copy link
Author

The changes have been made. Please do check.

Co-authored-by: Kelvin S. do Prado <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants