# binary-search-insert

> Binary search insert values into sorted arrays with comparator functions

Latest version **1.0.3** (published 2016-01-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install binary-search-insert
pnpm add binary-search-insert
yarn add binary-search-insert
bun add binary-search-insert
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2016-01-10 |
| First published | 2015-12-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Casey Flynn |
| Maintainers | cflynn07 |
| Keywords | array fast insert, array insert, binary search, sorted array |

## Links

- npm: https://www.npmjs.com/package/binary-search-insert
- npm.io page: https://npm.io/package/binary-search-insert

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2016-01-10
- 1.0.2 — 2016-01-06
- 1.0.1 — 2016-01-06
- 1.0.0 — 2016-01-06
- 0.2.2 — 2015-12-31
- 0.2.1 — 2015-12-31
- 0.2.0 — 2015-12-30
- 0.1.0 — 2015-12-30
- 0.0.0 — 2015-12-29

## README

binary-search-insert
====================
[![Build Status](https://travis-ci.org/cflynn07/binary-search-insert.svg)](https://travis-ci.org/cflynn07/binary-search-insert)
[![Code Climate](https://codeclimate.com/github/cflynn07/binary-search-insert/badges/gpa.svg)](https://codeclimate.com/github/cflynn07/binary-search-insert)
[![Test Coverage](https://codeclimate.com/github/cflynn07/binary-search-insert/badges/coverage.svg)](https://codeclimate.com/github/cflynn07/binary-search-insert)
[![Dependency Status](https://david-dm.org/cflynn07/binary-search-insert.svg)](https://david-dm.org/cflynn07/binary-search-insert)
[![devDependency Status](https://david-dm.org/cflynn07/binary-search-insert/dev-status.svg)](https://david-dm.org/cflynn07/binary-search-insert#info=devDependencies)

[![NPM](https://nodei.co/npm/binary-search-insert.png?compact=true)](https://nodei.co/npm/binary-search-insert/)

A library for performing a binary search and insert into a sorted array.  
Binary search has an average time complexity of O(log(n)) which is substantially faster than a linear search
with an average time complexity of O(n).

Installing
----------
```
$ npm install binary-search-insert
```

Usage
-----
```js
var binarySearchInsert = require('binary-search-insert');
var sortedArray = [1, 3, 5, 7, 9, 11];
var comparator = function (a, b) { return a - b; }

/**
 * Mutates sortedArray and returns index of inserted value
 * @param {Array} A sorted array
 * @param {*} An item to insert in the sorted array
 * @param {Function} A comparator function that takes two arguments and returns a number. The first
 *   argument will be a member of sortedArray, the second argument will be item.
 *   If item < member, return value < 0
 *   If item > member, return value > 0
 * @returns {Number} index of array where item is inserted
 */
var indexInsertedAt = binarySearchInsert(sortedArray, comparator, 6);
// indexInsertedAt: 3
// sortedArray: [1, 3, 5, 6, 7, 9, 11]
```

Benchmarks
----------
http://jsperf.com/cflynn07-binary-search-insert-vs-linear-search-insert  
![Performance](https://cloud.githubusercontent.com/assets/467885/12046878/7c2d98ca-ae76-11e5-8eee-34bb01c2e09b.png)

Testing
-------
```
// Tests + coverage reports are run using Lab
$ npm test
// Test coverage reports
$ npm run test-cov # will auto-open Google Chrome with html coverage report
```

License
-------
[MIT](https://raw.githubusercontent.com/cflynn07/binary-search-insert/master/LICENSE)

---
_Source: https://npm.io/package/binary-search-insert · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
