-
Notifications
You must be signed in to change notification settings - Fork 128
/
ternary_search.js
35 lines (30 loc) · 905 Bytes
/
ternary_search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Ternary Search Algorithm
* @param {Array} sortedArray Sorted Array to be searched
* @param {Number} element Element to be searched
* @return {Number} Index of the element, if found
*/
const ternarysearch = (sortedArray, element) => {
let left = 0;
let right = sortedArray.length - 1;
while (left <= right) {
const firstMid = (left) + Math.floor((right - left) / 3);
const secondMid = (firstMid) + Math.floor((right - left) / 3);
if (sortedArray[firstMid] === element) {
return firstMid;
}
if (sortedArray[secondMid] === element) {
return secondMid;
}
if (sortedArray[firstMid] > element) {
right = firstMid - 1;
} else if (sortedArray[secondMid] < element) {
left = secondMid + 1;
} else {
left = firstMid + 1;
right = secondMid - 1;
}
}
return -1;
};
module.exports = ternarysearch;