Javascript implementation of the above approach
var maxLen = 30 ; function upper_bound ( a , low , high , element ) { while ( low < high ) { var middle = low + parseInt ( ( high - low ) / 2 ) ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; }