bemoje-binary-array-iteration
Binary search and indexOf based on binary search as well. Works only on sorted arrays.
Features
- Binary search -based indexOf for sorted arrays of primitive values.
- Binary search -based indexOf for oject-arrays sorted by a property where it's value is guaranteed to be a primitive value.
- Binary search for sorted arrays of primitive values.
- Binary search for oject-arrays sorted by a property where it's value is guaranteed to be a primitive value..
Primitive values are instances of String, Number, Boolean or Null.
Install
$ npm install @bemoje/binary-search
Dependencies
- None
Example Usage
import { binaryIndexOf, binaryIndexOfObj, binarySearch, binarySearchObj } = from '@bemoje/binary-search'
const sortedArray = ["8g9a", "a", "aba", "b", "c", "d"];
console.log(arrBinaryIndexOf(sortedArray, "8g9a")); // => 0
console.log(arrBinaryIndexOf(sortedArray, "a")); // => 1
console.log(arrBinaryIndexOf(sortedArray, "aba")); // => 2
console.log(arrBinaryIndexOf(sortedArray, "b")); // => 3
console.log(arrBinaryIndexOf(sortedArray, "c")); // => 4
console.log(arrBinaryIndexOf(sortedArray, "d")); // => 5
console.log(arrBinarySearch(sortedArray, "8g9a")); // => { a: 'whatever', sortedProperty: '8g9a' }
console.log(arrBinarySearch(sortedArray, "a")); // => { a: 'whatever', sortedProperty: 'a' }
console.log(arrBinarySearch(sortedArray, "aba")); // => { a: 'whatever', sortedProperty: 'aba' }
console.log(arrBinarySearch(sortedArray, "b")); // => { a: 'whatever', sortedProperty: 'b' }
console.log(arrBinarySearch(sortedArray, "c")); // => { a: 'whatever', sortedProperty: 'c' }
console.log(arrBinarySearch(sortedArray, "d")); // => { a: 'whatever', sortedProperty: 'd' }
const objectArray = [
{ a: "whatever", sortedProperty: "8g9a" },
{ a: "whatever", sortedProperty: "a" },
{ a: "whatever", sortedProperty: "aba" },
{ a: "whatever", sortedProperty: "b" },
{ a: "whatever", sortedProperty: "c" },
{ a: "whatever", sortedProperty: "d" }
];
console.log(objArrBinaryIndexOf(objectArray, "sortedProperty", "8g9a")); // => 0
console.log(objArrBinaryIndexOf(objectArray, "sortedProperty", "a")); // => 1
console.log(objArrBinaryIndexOf(objectArray, "sortedProperty", "aba")); // => 2
console.log(objArrBinaryIndexOf(objectArray, "sortedProperty", "b")); // => 3
console.log(objArrBinaryIndexOf(objectArray, "sortedProperty", "c")); // => 4
console.log(objArrBinaryIndexOf(objectArray, "sortedProperty", "d")); // => 5
console.log(objArrBinarySearch(objectArray, "sortedProperty", "8g9a")); // => { a: 'whatever', sortedProperty: '8g9a' }
console.log(objArrBinarySearch(objectArray, "sortedProperty", "a")); // => { a: 'whatever', sortedProperty: 'a' }
console.log(objArrBinarySearch(objectArray, "sortedProperty", "aba")); // => { a: 'whatever', sortedProperty: 'aba' }
console.log(objArrBinarySearch(objectArray, "sortedProperty", "b")); // => { a: 'whatever', sortedProperty: 'b' }
console.log(objArrBinarySearch(objectArray, "sortedProperty", "c")); // => { a: 'whatever', sortedProperty: 'c' }
console.log(objArrBinarySearch(objectArray, "sortedProperty", "d")); // => { a: 'whatever', sortedProperty: 'd' }