Skip to content

addyosmani/recursive-binarysearch

Repository files navigation

NPM version Build Status Dependency Status

Recursive Binary Search

A recursive Binary Search implementation with O(log N) complexity based on javascript-algorithms.

A straightforward implementation of binary search is recursive. The initial call uses the indices of the entire array to be searched. The procedure then calculates an index midway between the two indices, determines which of the two subarrays to search, and then does a recursive call to search that subarray.

Install

$ npm install --save recursive-binarysearch

Usage

var binarysearch = require('recursive-binarysearch');

binarySearch([1, 2, 3, 4, 6, 8], 1);
// => 0

binarySearch([1, 2, 3, 4, 6, 8], 4);
// => 3

binarySearch([1, 8], 1);
// => 0

binarySearch([1, 2, 3], 4);
// => -1

binarySearch([], 4);
// => -1

License

MIT © Addy Osmani

About

Recursive Binary Search with O(log N) complexity

Resources

Stars

Watchers

Forks

Packages

No packages published