Skip to content

Instantly share code, notes, and snippets.

@AlexandrFadeev
Created September 12, 2017 12:13
Show Gist options
  • Save AlexandrFadeev/0a33aa546e503bd69d6f27ebbb9a2d46 to your computer and use it in GitHub Desktop.
Save AlexandrFadeev/0a33aa546e503bd69d6f27ebbb9a2d46 to your computer and use it in GitHub Desktop.
Binary search algorithm in javascript
/**
Binary search algorithm
*/
// -------------------------------------------------------------------------------------------------
/**
Helper function that generates array of numbers from 0 to (n) elements
*/
function generateNumbersUpTo(num) {
/**
Create array that holds all numbers.
*/
var numbersArray = [];
/**
Check if passed parameter if not 'Not a Number' (is number actualy)
then iterate in 'for' loop from 0 to passed number and adds 'i' iterator to
'numberArray'.
If condition fails (is Not a Number) than return NaN...
*/
if (!isNaN(num)) {
for (var i = 0; i < num; i++) {
numbersArray.push(i);
}
} else {
return NaN;
}
/**
return numbers array
*/
return numbersArray;
}
/**
Implementation of binary search algorithm
Attension! In order to this algorithm work properly, passed array of numbers should be sorted ascending
(0, 1, 2, ... n). In this algorithm we not sort passed array.
*/
function isElementExist(elements, searchedElement) {
/**
Check if passed parameter is NOT type of Array instance. If so -> returns 'false'.
Otherwise continue algorithm.
*/
if (!(elements instanceof Array)) {
console.log("Not an array");
return false
}
/**
Create two varibles for left index and right index. This need because we will check
only half of array (left or right).
'leftIndex' initialized to 0, and 'rightIndex' initialized to number of elements of array - 1.
Actualy 'rightIndex' is an end of array and 'leftIndex' is starting point of an passed array as a parameter.
*/
var leftIndex = 0;
var rightIndex = elements.length - 1;
/**
Loop via 'while' until condition '(leftIndex <= rightIndex)' returns 'true'
*/
while (leftIndex <= rightIndex) {
/**
Create two constants. Constant 'middleIndex' that holds middleIndex of array.
Calculation of 'middleIndex' is a little bit tricky...
'leftIndex' adds to 'rightIndex' which is 0 + 9 (if passed array of 10 elements).
After that we shift 1 byte from number 9 to the right, which is equal to 9 / 2...
In bynary 9 is 1001. If we shit one byte to the right
this binary number will be 100. This bynary number is equal to 4.
After calculating 'middleIndex' of passed array we need actual value from passed array
at this 'middleIndex'.
*/
const middleIndex = (leftIndex + rightIndex) >> 1;
const middleValue = elements[middleIndex];
/**
Check if the searched number is equal to element of array at 'middleIndex'
then this number finded and return 'true'
*/
if (searchedElement == middleValue) {
return true
}
/**
Check if the searched number is lower than value of array at 'middleIndex'
then subtract by one from 'rightIndex'
*/
if (searchedElement < middleValue) {
rightIndex = middleIndex - 1;
}
/**
Check if the searched number is higher than value of array at 'middleIndex'
then adds by one to 'leftIndex'
*/
if (searchedElement > middleValue) {
leftIndex = middleIndex + 1;
}
console.log("middleValue " + middleValue);
}
/**
Number not fined. Return 'false'
*/
return false
}
/**
Usage. First parameter of 'isElementExist' function is ascended sorted array from 0 to ...n elements.
For this reason we call helper function 'generateNumbersUpTo(n)'
Second parameter is the number we want to find.
*/
console.log(isElementExist(generateNumbersUpTo(10000), 9999))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment