Skip to content

Commit b078444

Browse files
committed
feat: add sentinel search algorithm
1 parent 9010481 commit b078444

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

Search/QuickSelectSearch.js

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

1919
let from = 0
2020
let to = array.length - 1
21+
2122
while (from < to) {
22-
let left = from
23-
let right = to
23+
let [left, right] = [from, to]
24+
2425
const pivot = array[Math.ceil((left + right) * 0.5)]
2526

2627
while (left < right) {
2728
if (array[left] >= pivot) {
28-
const tmp = array[left]
29-
array[left] = array[right]
30-
array[right] = tmp
29+
;[array[left], array[right]] = [array[right], array[left]]
3130
--right
3231
} else {
3332
++left

Search/SentinelSearch.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
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
6+
* find a target value in an array or list. The basic idea behind this algorithm is to add a
7+
* sentinel value at the end of the array which is equal to the target value we are looking for.
8+
* This helps to avoid checking the array boundary condition during each iteration of the loop,
9+
* as the sentinel value acts as a stopper for the loop.
10+
*
11+
* @param {number[]} array - sorted list of numbers
12+
* @param {number} target - target number to search for
13+
* @return {number | null} - index of the target number in the list, or null if not found
14+
* @see [SentinelSearch](https://www.geeksforgeeks.org/sentinel-linear-search/)
15+
* @example sentinelSearch([1,2,3], 2) => 1
16+
* @example sentinelSearch([4,5,6], 2) => null
17+
* @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).
22+
* Auxiliary Space: O(1)
23+
*/
24+
25+
export const sentinelSearch = (array, target) => {
26+
const arrayLength = array.length
27+
if (arrayLength === 0) return null
28+
29+
// Element to be searched is placed at the last index
30+
const last = array[arrayLength - 1]
31+
array[arrayLength - 1] = target
32+
33+
let index = 0
34+
while (array[index] !== target) index++
35+
36+
// Put the last element back
37+
array[arrayLength - 1] = last
38+
39+
if (index < arrayLength - 1 || array[arrayLength - 1] === target) return index
40+
return null
41+
}

Search/test/SentinelSearch.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { sentinelSearch } from '../SentinelSearch'
2+
3+
describe('Sentinel search', () => {
4+
test.each([
5+
[['o', 'b', 'c'], 'c', 2],
6+
[[1, 2, 3, 4, 5], 4, 3],
7+
[['s', 't', 'r', 'i', 'n', 'g'], 'a', null],
8+
[['1', '2', '3'], '1', 0],
9+
[['4', 'e', '6', '10'], 4, null]
10+
])('of %o , searching for %o, expected %i', (array, target, index) => {
11+
expect(sentinelSearch(array, target)).toStrictEqual(index)
12+
})
13+
})

0 commit comments

Comments
 (0)