Skip to content

Instantly share code, notes, and snippets.

View foxymiles's full-sized avatar

Miles Egan foxymiles

View GitHub Profile
@foxymiles
foxymiles / binary_search.ts
Created March 4, 2022 13:39
Binary Search in TS
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;
}