INNER CODE UNIT · JavaScript

mid

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

        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
        if (closestIndex === -1 || Math.abs(functor(arr[mid]) - target) < Math.abs(functor(arr[closestIndex]) - target)) {
            closestIndex = mid;
        }
    }

    return closestIndex; // Target not found, return the closest index

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…