/bsc

🔍 Binary search with comparator

Primary LanguageTypeScriptMIT LicenseMIT

bsc

⚠️ Project has been transferred to NexTools metarepo

npm tests coverage

Binary search with comparator.

  • tests not only for the result but for how many steps it takes as well
  • always return -1 if nothing has been found, no "insertion point" negative values
  • no "lower" or "upper bounds"
  • ESM

Requirements

Install

$ yarn add bsc
# or
$ npm install bsc

Usage

<A, B>(arr: A[], key: B, comparator: (key: B, mid: A) => number) => number
import binarySearch from 'bsc'

const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const comparator = (key, mid) => key - mid

binarySearch(arr, 2, comparator) // 2
binarySearch(arr, 10, comparator) // -1