Skip to content

Commit

Permalink
Implement 2 versions of binary search in binarySearchArray.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Dec 28, 2014
1 parent de93247 commit 46fb517
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion binarySearchArray/binarySearchArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,65 @@ exhibits the desired characteristics.
**/

var binarySearch = function (array, element) {

var length, mid, midValue, left, right, result;

// check for allowable conditions
if ( Array.isArray(array) && typeof element === 'number' ) {
length = array.length;
mid = Math.floor( length / 2 );
midValue = array[mid];
left = array.slice(0, mid);
right = array.slice(mid + 1);
result = 0;

if (length === 0) {
// base condition
return null;
// result = null;
} else if ( midValue === element ) {
return mid;
// result = mid;
} else if ( midValue > element ) {
return binarySearch( left, element );
// result = binarySearch( left, element );
} else {
var temp = binarySearch( right, element );
// result = temp === null ? temp : mid + temp + 1;
return temp === null ? temp : mid + temp + 1;
}
// return result;
}
};


// Space Complexity O(log n) b/c of recursive stack
var binarySearch = function (array, element) {
var inner = function (lowerBound, upperBound) {
var mid = Math.floor( (lowerBound + upperBound) / 2 );
var midValue = array[mid];

/*
* base condition: if the mid index is either of the bounds,
* then if neither of the array values at the index doesn't
* equal the value, then the value can't be found in the array
*/
if ( mid === lowerBound || mid === upperBound ) {
return midValue === element ? mid : null;
} else if (midValue === element) {
return mid;
} else if (midValue > element) {
return inner(lowerBound, mid);
} else {
return inner(mid, upperBound);
}
};

// check for allowable conditions
if ( !Array.isArray(array) ) {
throw new Error('Need to pass in a valid Array object');
} else if ( typeof element !== 'number' ) {
throw new Error('Need to pass in a valid Number to find in Array');
} else {
return inner(0, array.length);
}
};

0 comments on commit 46fb517

Please sign in to comment.