-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
35 lines (30 loc) · 1008 Bytes
/
index.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
import {binarySearch} from '../binary-search/index.js';
/**
* Returns the index of the search element from the given array.
*
* @param {string} sortOrder - 'asc' or 'desc'
* @param {Array<number>} sortedArray
* @param {number} searchWord
*
* @returns {number}
*/
function exponentialSearch(sortOrder, sortedArray, searchValue) {
if (sortedArray[0] === searchValue) {
return sortedArray[0];
}
let i = 1;
const length = sortedArray.length - 1;
// Iterate over the whole array in a exponential meaning until the value is bigger than the value we are looking for
while (i < length && sortedArray[i] <= searchValue) {
i *= 2;
}
// Math min is required in the case "i" is bigger than the array length.
// "i" does get divided by 2 because the current index "i" is bigger than the searched value
return binarySearch(
sortOrder,
sortedArray,
Math.floor(i / 2),
Math.min(i, length),
searchValue
);
}