INNER CODE UNIT · JavaScript

binarySearchClosest

sirius-ms/sirius · lcms2/viewer/viewer.js:20

function binarySearchClosest(arr, target,functor) {
    let low = 0;
    let high = arr.length - 1;
    let closestIndex = -1;

    while (low <= high) {
        const mid = Math.floor((low + high) / 2);
        const guess = functor(arr[mid]);

        if (guess === target) {
            return mid; // Target found, return its index
        } else if (guess < target) {
            low = mid + 1; // Target is in the right half
        } else {
            high = mid - 1; // Target is in the left half
        }

        // Update the closest index if needed

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…