function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare, aBias) {
            // This function terminates when one of the following is true:
            //
            //   1. We find the exact element we are looking for.
            //
            //   2. We did not find the exact element, but we can return the index of
            //      the next-closest element.
            //
            //   3. We did not find the exact element, and there is no next-closest
            //      element than the one we are searching for, so we return -1.
            var mid = Math.floor((aHigh - aLow) / 2) + aLow;
            var cmp = aCompare(aNeedle, aHaystack[mid], true);
            if (cmp === 0) {
                // Found the element we are looking for.
                return mid;
            }
            else if (cmp > 0) {
                // Our needle is greater than aHaystack[mid].
                if (aHigh - mid > 1) {
                    // The element is in the upper half.
                    return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare, aBias);
                }
                // The exact needle element was not found in this haystack. Determine if
                // we are in termination case (3) or (2) and return the appropriate thing.
                if (aBias == exports.LEAST_UPPER_BOUND) {
                    return aHigh < aHaystack.length ? aHigh : -1;
                }
                else {
                    return mid;
                }
            }
            else {
                // Our needle is less than aHaystack[mid].
                if (mid - aLow > 1) {
                    // The element is in the lower half.
                    return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare, aBias);
                }
                // we are in termination case (3) or (2) and return the appropriate thing.
                if (aBias == exports.LEAST_UPPER_BOUND) {
                    return mid;
                }
                else {
                    return aLow < 0 ? -1 : aLow;
                }
            }
        }
/**
         * Recursive implementation of binary search.
         *
         * @param aLow Indices here and lower do not contain the needle.
         * @param aHigh Indices here and higher do not contain the needle.
         * @param aNeedle The element being searched for.
         * @param aHaystack The non-empty array being searched.
         * @param aCompare Function which takes two elements and returns -1, 0, or 1.
         * @param aBias Either 'binarySearch.GREATEST_LOWER_BOUND' or
         *     'binarySearch.LEAST_UPPER_BOUND'. Specifies whether to return the
         *     closest element that is smaller than or greater than the one we are
         *     searching for, respectively, if the exact element cannot be found.
         */
Recursive implementation of binary search.
@param aLow Indices here and lower do not contain the needle.
@param aHigh Indices here and higher do not contain the needle.
@param aNeedle The element being searched for.
@param aHaystack The non-empty array being searched.
@param aCompare Function which takes two elements and returns -1, 0, or 1.
Recursive implementation of binary search . @param aLow Indices here and lower do not contain the needle . @param aHigh Indices here and higher do not contain the needle . @param aNeedle The element being searched for . @param aHaystack The non - empty array being searched . @param aCompare Function which takes two elements and returns - 1 0 or 1 .
function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare, aBias) {
               1. We find the exact element we are looking for.
               2. We did not find the exact element, but we can return the index of
                  the next-closest element.
               3. We did not find the exact element, and there is no next-closest
                  element than the one we are searching for, so we return -1.
            var mid = Math.floor((aHigh - aLow) / 2) + aLow;
            var cmp = aCompare(aNeedle, aHaystack[mid], true);
            if (cmp === 0) {
                 Found the element we are looking for.
                return mid;
            }
            else if (cmp > 0) {
                 Our needle is greater than aHaystack[mid].
                if (aHigh - mid > 1) {
                     The element is in the upper half.
                    return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare, aBias);
                }
                 The exact needle element was not found in this haystack. Determine if
                 we are in termination case (3) or (2) and return the appropriate thing.
                if (aBias == exports.LEAST_UPPER_BOUND) {
                    return aHigh < aHaystack.length ? aHigh : -1;
                }
                else {
                    return mid;
                }
            }
            else {
                 Our needle is less than aHaystack[mid].
                if (mid - aLow > 1) {
                     The element is in the lower half.
                    return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare, aBias);
                }
                 we are in termination case (3) or (2) and return the appropriate thing.
                if (aBias == exports.LEAST_UPPER_BOUND) {
                    return mid;
                }
                else {
                    return aLow < 0 ? -1 : aLow;
                }
            }
        }
function recursiveSearch ( aLow , aHigh , aNeedle , aHaystack , aCompare , aBias ) { var mid = Math . floor ( ( aHigh - aLow ) / 2 ) + aLow ; var cmp = aCompare ( aNeedle , aHaystack [ mid ] , true ) ; if ( cmp === 0 ) { return mid ; } else if ( cmp > 0 ) { if ( aHigh - mid > 1 ) { return recursiveSearch ( mid , aHigh , aNeedle , aHaystack , aCompare , aBias ) ; } if ( aBias == exports . LEAST_UPPER_BOUND ) { return aHigh < aHaystack . length ? aHigh : - 1 ; } else { return mid ; } } else { if ( mid - aLow > 1 ) { return recursiveSearch ( aLow , mid , aNeedle , aHaystack , aCompare , aBias ) ; } if ( aBias == exports . LEAST_UPPER_BOUND ) { return mid ; } else { return aLow < 0 ? - 1 : aLow ; } } }
Recursive implementation of binary search.
Recursive implementation of binary search .
// This function terminates when one of the following is true: // //   1. We find the exact element we are looking for. // //   2. We did not find the exact element, but we can return the index of //      the next-closest element. // //   3. We did not find the exact element, and there is no next-closest //      element than the one we are searching for, so we return -1. // Found the element we are looking for. // Our needle is greater than aHaystack[mid]. // The element is in the upper half. // The exact needle element was not found in this haystack. Determine if // we are in termination case (3) or (2) and return the appropriate thing. // Our needle is less than aHaystack[mid]. // The element is in the lower half. // we are in termination case (3) or (2) and return the appropriate thing.