Skip to content

binary-algorithm-search is an npm package that provides an efficient implementation of the binary search algorithm. It can be used to quickly search for items in a sorted list.

Notifications You must be signed in to change notification settings

DavySz/binary-algorithm-search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

Param Value
list Array of sorted numbers
target Number to be found
return Index 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:

Param Value
list Array of unsorted numbers
return Sorted list

Made with ❤️ by Davy de Souza

About

binary-algorithm-search is an npm package that provides an efficient implementation of the binary search algorithm. It can be used to quickly search for items in a sorted list.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published