Skip to content

feat: add sentinel search algorithm #1683

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Search/QuickSelectSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ export function quickSelectSearch(array, k) {

let from = 0
let to = array.length - 1

while (from < to) {
let [left, right] = [from, to]
let left = from
Copy link
Collaborator

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.

let right = to

const pivot = array[Math.ceil((left + right) * 0.5)]

while (left < right) {
if (array[left] >= pivot) {
;[array[left], array[right]] = [array[right], array[left]]
const tmp = array[left]
array[left] = array[right]
array[right] = tmp
--right
} else {
++left
Expand Down
10 changes: 2 additions & 8 deletions Search/SentinelSearch.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* @function sentinelSearch
* @description Sentinel search algorithm for array.
*
* Sentinel linear search is a variation of the standard linear search algorithm used to
* @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,
Expand All @@ -15,10 +12,7 @@
* @example sentinelSearch([1,2,3], 2) => 1
* @example sentinelSearch([4,5,6], 2) => null
* @complexity_analysis
* Time Complexity :
* Worst Case -> The time complexity of the Sentinel Linear Search algorithm is O(n) in the worst case.
* Best Case -> In the best case, when the key is found in the first iteration, the time complexity will be O(1).
* Average Case -> However, the average time complexity is still O(n).
* Time Complexity : O(n)
* Auxiliary Space: O(1)
*/

Expand Down