-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
feat: add sentinel search algorithm #1683
Open
hasanalkaf3
wants to merge
4
commits into
TheAlgorithms:master
Choose a base branch
from
hasanalkaf3:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @description Sentinel linear search is a variation of the standard linear search algorithm used to | ||
* find a target value in an array or list. The basic idea behind this algorithm is to add a | ||
* sentinel value at the end of the array which is equal to the target value we are looking for. | ||
* This helps to avoid checking the array boundary condition during each iteration of the loop, | ||
* as the sentinel value acts as a stopper for the loop. | ||
* | ||
* @param {number[]} array - sorted list of numbers | ||
* @param {number} target - target number to search for | ||
* @return {number | null} - index of the target number in the list, or null if not found | ||
* @see [SentinelSearch](https://www.geeksforgeeks.org/sentinel-linear-search/) | ||
* @example sentinelSearch([1,2,3], 2) => 1 | ||
* @example sentinelSearch([4,5,6], 2) => null | ||
* @complexity_analysis | ||
* Time Complexity : O(n) | ||
* Auxiliary Space: O(1) | ||
*/ | ||
|
||
export const sentinelSearch = (array, target) => { | ||
const arrayLength = array.length | ||
if (arrayLength === 0) return null | ||
|
||
// Element to be searched is placed at the last index | ||
const last = array[arrayLength - 1] | ||
array[arrayLength - 1] = target | ||
|
||
let index = 0 | ||
while (array[index] !== target) index++ | ||
|
||
// Put the last element back | ||
array[arrayLength - 1] = last | ||
|
||
if (index < arrayLength - 1 || array[arrayLength - 1] === target) return index | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { sentinelSearch } from '../SentinelSearch' | ||
|
||
describe('Sentinel search', () => { | ||
test.each([ | ||
[['o', 'b', 'c'], 'c', 2], | ||
[[1, 2, 3, 4, 5], 4, 3], | ||
[['s', 't', 'r', 'i', 'n', 'g'], 'a', null], | ||
[['1', '2', '3'], '1', 0], | ||
[['4', 'e', '6', '10'], 4, null] | ||
])('of %o , searching for %o, expected %i', (array, target, index) => { | ||
expect(sentinelSearch(array, target)).toStrictEqual(index) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep unrelated changes out of this PR.