3.2.0 • Published 11 months ago

binary-algorithm-search v3.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

Description

The binary-algorithm-search library from npm is an implementation of the binary search algorithm. Binary search is an efficient search algorithm that finds the position of a target value within a sorted array. Binary search works by repeatedly dividing the array in half until the target value is found or until the subarray becomes so small that the target value is known not to be in it.

How to use

The library exports two main methods:

Search

import { search } from 'binary-algorithm-search'

search([1, 2, 3, 4, 5], 3);  // expected return: 2

Signature:

search(list: number[], target: number): number

Params:

ParamValue
listArray of sorted numbers
targetNumber to be found
returnIndex of the target value.

Sort

The search method assumes that the array passed as the first argument is already sorted for performance reasons. However, if you need to sort the array before using the search method, you can simply import the sort method and pass the list to it:

import { sort } from 'binary-algorithm-search'

sort([5, 4, 3, 2, 1]);  // expected return: [1, 2, 3, 4, 5]

Signature:

sort(list: number[]): number[]

Params:

ParamValue
listArray of unsorted numbers
returnSorted list

1.2.0

11 months ago

1.1.0

11 months ago

3.2.0

11 months ago

3.1.0

11 months ago

3.0.1

11 months ago

1.6.2

11 months ago

1.6.1

11 months ago

3.0.0

11 months ago

1.6.0

11 months ago

1.5.0

11 months ago

1.4.0

11 months ago

1.3.0

11 months ago

2.0.0

11 months ago

1.0.0

11 months ago