Skip to content

Commit 8d7d6ea

Browse files
committed
chore: update SentinelSearch.js
1 parent 163f1c0 commit 8d7d6ea

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

Search/QuickSelectSearch.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ export function quickSelectSearch(array, k) {
1818

1919
let from = 0
2020
let to = array.length - 1
21-
2221
while (from < to) {
23-
let [left, right] = [from, to]
22+
let left = from
23+
let right = to
2424

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

2727
while (left < right) {
2828
if (array[left] >= pivot) {
29-
;[array[left], array[right]] = [array[right], array[left]]
29+
const tmp = array[left]
30+
array[left] = array[right]
31+
array[right] = tmp
3032
--right
3133
} else {
3234
++left

Search/SentinelSearch.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
/**
2-
* @function sentinelSearch
3-
* @description Sentinel search algorithm for array.
4-
*
5-
* Sentinel linear search is a variation of the standard linear search algorithm used to
2+
* @description Sentinel linear search is a variation of the standard linear search algorithm used to
63
* find a target value in an array or list. The basic idea behind this algorithm is to add a
74
* sentinel value at the end of the array which is equal to the target value we are looking for.
85
* This helps to avoid checking the array boundary condition during each iteration of the loop,
@@ -15,10 +12,7 @@
1512
* @example sentinelSearch([1,2,3], 2) => 1
1613
* @example sentinelSearch([4,5,6], 2) => null
1714
* @complexity_analysis
18-
* Time Complexity :
19-
* Worst Case -> The time complexity of the Sentinel Linear Search algorithm is O(n) in the worst case.
20-
* Best Case -> In the best case, when the key is found in the first iteration, the time complexity will be O(1).
21-
* Average Case -> However, the average time complexity is still O(n).
15+
* Time Complexity : O(n)
2216
* Auxiliary Space: O(1)
2317
*/
2418

0 commit comments

Comments
 (0)