Skip to content

Commit

Permalink
chore: update SentinelSearch.js
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanalkaf3 committed Oct 3, 2024
1 parent 163f1c0 commit 8d7d6ea
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
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
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

0 comments on commit 8d7d6ea

Please sign in to comment.