boundit
Version:
🔍 JavaScript and TypeScript implementation of lower_bound and upper_bound for efficient data searching.
29 lines (25 loc) • 1.52 kB
text/typescript
/**
* Binary search is a fast search algorithm with run-time complexity of O(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form.
*
* @param {number[]} array - a sorted array
* @param {number} target - the target value to search for
* @returns {number} - the index of the target value in the array, or -1 if the target value is not found.
*/
declare const binarySearch: (array: number[], target: number) => number;
/**
* Returns the index of the first element in the array which is greater than or equal to the target.
*
* @param {number[]} array - a sorted array
* @param {number} target - the target value to search for
* @returns {number} - the index of the first element in the array which is greater than or equal to the target, or the length of the array if no such element is found.
*/
declare const lowerBound: (array: number[], target: number) => number;
/**
* Returns the index of the first element in the array which is greater than the target, or the length of the array if no such element is found.
*
* @param {number[]} array - a sorted array
* @param {number} target - the target value to search for
* @returns {number} - the index of the first element in the array which is greater than the target, or the length of the array if no such element is found.
*/
declare const upperBound: (array: number[], target: number) => number;
export { binarySearch, lowerBound, upperBound };