binary-search-asim
v1.0.1
Published
This npm package provides a simple implementation of the binary search algorithm which is used to search in `sorted` array.
Readme
Binary Search Package
This npm package provides a simple implementation of the binary search algorithm which is used to search in sorted array.
Installation
To install this package, run the following command:
npm install binary-search-asim
Usage
After installing the package, you can import and use the binary search function as follows:
- import anyName from
"binary-search-asim" - To run this --> In
package.jsonadd"type":"module"OR make filename with.mjsextension
import binarySearch from "binary-search-asim";
const sortedArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const target = 5;
const index = binarySearch(sortedArray, target);
console.log(index); // Output: 4Function
binarySearch(arr, target)
- Description: Searches for the
targetvalue in a sorted arrayarrand returns its index. If thetargetis not found, it returns-1. - Parameters:
arr(Array): The sorted array to search in.target(Number): The value to search for. - Returns:
Number: The index of thetargetin the array or-1if not found.
