-
Notifications
You must be signed in to change notification settings - Fork 31.1k
Expand file tree
/
Copy pathternarySearch.js
More file actions
53 lines (46 loc) · 1.84 KB
/
ternarySearch.js
File metadata and controls
53 lines (46 loc) · 1.84 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import Comparator from '../../../utils/comparator/Comparator';
/**
* Ternary search implementation.
*
* Ternary search divides the array into three parts and determines which
* part the element is likely to be in, then recursively searches that part.
*
* Time Complexity: O(log3(n))
*
* @param {*[]} sortedArray - A sorted array to search within.
* @param {*} seekElement - The element we are searching for.
* @param {function(a, b)} [comparatorCallback] - Optional custom comparator.
* @return {number} - Index of the element, or -1 if not found.
*/
export default function ternarySearch(sortedArray, seekElement, comparatorCallback) {
const comparator = new Comparator(comparatorCallback);
let startIndex = 0;
let endIndex = sortedArray.length - 1;
while (startIndex <= endIndex) {
// Divide the range into three equal parts.
const partSize = Math.floor((endIndex - startIndex) / 3);
const midLeftIndex = startIndex + partSize;
const midRightIndex = endIndex - partSize;
// Check if the element is at the left midpoint.
if (comparator.equal(sortedArray[midLeftIndex], seekElement)) {
return midLeftIndex;
}
// Check if the element is at the right midpoint.
if (comparator.equal(sortedArray[midRightIndex], seekElement)) {
return midRightIndex;
}
if (comparator.lessThan(seekElement, sortedArray[midLeftIndex])) {
// The element is in the first third of the array.
endIndex = midLeftIndex - 1;
} else if (comparator.greaterThan(seekElement, sortedArray[midRightIndex])) {
// The element is in the last third of the array.
startIndex = midRightIndex + 1;
} else {
// The element is in the middle third of the array.
startIndex = midLeftIndex + 1;
endIndex = midRightIndex - 1;
}
}
// Return -1 if we have not found anything.
return -1;
}