Uniform Binary Search | Javascript implementation of above approach ; lookup table ; Create the lookup table for an array of length n ; Power and count variable ; Multiply by 2 ; Initialize the lookup table ; Binary search ; mid point of the array ; count ; If the value is found ; If value is less than the mid value ; If value is greater than the mid value ; Driver code ; Create the lookup table ; Print the position of the array
let MAX_SIZE = 1000 ; let lookup_table = new Array ( MAX_SIZE ) ; lookup_table . fill ( 0 ) ; function create_table ( n ) { let pow = 1 ; let co = 0 ; while ( true ) { pow <<= 1 ; lookup_table [ co ] = parseInt ( ( n + ( pow >> 1 ) ) / pow , 10 ) ; if ( lookup_table [ co ++ ] == 0 ) { break ; } } } function binary ( arr , v ) { let index = lookup_table [ 0 ] - 1 ; let co = 0 ; while ( lookup_table [ co ] != 0 ) { if ( v == arr [ index ] ) return index ; else if ( v < arr [ index ] ) { index -= lookup_table [ ++ co ] ; return index ; } else { index += lookup_table [ ++ co ] ; return index ; } } return index ; } let arr = [ 1 , 3 , 5 , 6 , 7 , 8 , 9 ] ; let n = arr . length ; create_table ( n ) ; document . write ( " " + binary ( arr , 3 ) ) ;