This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function binarySearch<T>(values: T[], compare: (v: T) => number): number { | |
let left = 0; | |
let right = values.length - 1; | |
let ans = -1; | |
while (left <= right) { | |
const mid = Math.floor(left + (right - left) / 2); | |
const comp = compare(values[mid]!); | |
if (comp === 0) { | |
return mid; | |
} |