Function binarySearch

  • Returns the index of x in a sorted array if found, in O(log n) using binary search. If the element is not found, returns a negative integer.


    The index if the element is found or a negative integer.


    • arr: any[]

      The array to sort

    • x: any

      The element to find

    • lo: number = 0

      The starting index

    • Optional hi: any

      The end index to search within

    • comp: ((x: any, y: any) => number) = compare

      The compare function to check for x

        • (x: any, y: any): number
        • Checks whether x is a comparable type and returns the result of Otherwise the return value of the compare function is equivalent to obj === other ? 0 : obj < other ? -1 : 1


          The comparison result



          • x: any

            An initial value

          • y: any

            Other value to compare

          Returns number

    Returns number

Generated using TypeDoc