Resources
This commit is contained in:
28
resources/js/binaryIndexOf.js
Normal file
28
resources/js/binaryIndexOf.js
Normal file
@ -0,0 +1,28 @@
|
||||
function binaryIndexOf(searchElement) {
|
||||
'use strict';
|
||||
|
||||
var minIndex = 0;
|
||||
var maxIndex = this.length - 1;
|
||||
var currentIndex;
|
||||
var currentElement;
|
||||
var resultIndex;
|
||||
|
||||
while (minIndex <= maxIndex) {
|
||||
resultIndex = currentIndex = (minIndex + maxIndex) / 2 | 0;
|
||||
currentElement = this[currentIndex];
|
||||
|
||||
if (currentElement < searchElement) {
|
||||
minIndex = currentIndex + 1;
|
||||
}
|
||||
else if (currentElement > searchElement) {
|
||||
maxIndex = currentIndex - 1;
|
||||
}
|
||||
else {
|
||||
return currentIndex;
|
||||
}
|
||||
}
|
||||
|
||||
return ~maxIndex;
|
||||
}
|
||||
|
||||
Array.prototype.binaryIndexOf = binaryIndexOf;
|
Reference in New Issue
Block a user