You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
679 B
24 lines
679 B
var isSorted = require('./isSorted'); |
|
exports = function(arr, val) { |
|
var cmp = |
|
arguments.length > 2 && arguments[2] !== undefined |
|
? arguments[2] |
|
: isSorted.defComparator; |
|
var startIdx = 0; |
|
var endIdx = arr.length - 1; |
|
while (startIdx <= endIdx) { |
|
var middleIdx = startIdx + Math.floor((endIdx - startIdx) / 2); |
|
var middleVal = arr[middleIdx]; |
|
if (cmp(middleVal, val) === 0) { |
|
return middleIdx; |
|
} |
|
if (cmp(middleVal, val) < 0) { |
|
startIdx = middleIdx + 1; |
|
} else { |
|
endIdx = middleIdx - 1; |
|
} |
|
} |
|
return -1; |
|
}; |
|
|
|
module.exports = exports;
|
|
|