binary-search-variations

2.0.4 • Public • Published

binary-search-variations

Don't want to write lengthy binary search algorithms in your code? Then this package is for you. binary-search-variations is an npm package that contains all important variations of the binary search algorithm, which you can implement in your code with just one line of code.

Read the documentation below to know more about the package.

Installation

npm i binary-search-variations

Usage

const { findGreatestLess } = 'binary-search-variations'

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const target = 5

const index = findGreatestLess(arr, target)
console.log(index) // 4

The functionalities included in this package -

  • typea a. Normal Binary Search(normalBinarySearch): This is the normal binary search algorithm which returns the index of the element if found, else returns -1.

    b. Find Lower Bound(findLowerBound): This function returns the index of the first element which is greater than or equal to the given element. If no such element is found, it returns -1.

    c. Find Upper Bound(findUpperBound): This function returns the index of the first element which is greater than the given element. If no such element is found, it returns -1.

    d. Find Least Greater(findLeastGreater): This function returns the index of the least element which is greater than the given element. If no such element is found, it returns -1.

    e. Find Greatest Lesser(findGreatestLesser): This function returns the index of the greatest element which is lesser than the given element. If no such element is found, it returns -1.

  • typeb: a. Find First and Last Occurence in an Array(searchTargetRange): This function returns the first and last occurence of the target element in the array. If the target element is not present in the array, it returns [-1, -1].

    b. Find Median of Two Arrays(medianOfArrays): This function returns the median of two sorted arrays.

    c. Search in 2D Matrix(search2DArr): It will return true if the target element is present in the 2D matrix, else it will return false.

    d. Find Peak Element(findPeakElement): This function returns the index of the peak element in the array. A peak element is an element which is greater than its neighbours.

  • typec: a. Find Missing Number(findMisingNumber): This function returns the missing number in the array. The array should contain numbers from 1 to n, with one number missing.

Package Sidebar

Install

npm i binary-search-variations

Weekly Downloads

3

Version

2.0.4

License

ISC

Unpacked Size

8.1 kB

Total Files

6

Last publish

Collaborators

  • aksabar