function binarySearch(arr, searchElement) {

	// Set starting values
	var currentElement;
	var currentIndex;
	var maxIndex = arr.length - 1;
	var minIndex = 0;

	while (minIndex <= maxIndex) {

		// Get a position near the middle
		currentIndex = Math.floor((minIndex + maxIndex) / 2);

		// Get the element at that index
		currentElement = arr[currentIndex];
		console.log("Start Index: ", minIndex);
		console.log("End Index: ", maxIndex);
		console.log("Current Element: ", currentElement);

		// If the search element is greater than the current element, look above this value
		if (currentElement < searchElement) {
			minIndex = currentIndex + 1;
		// If the search element is less than the current element, look below this value
		} else if (currentElement > searchElement) {
			maxIndex = currentIndex - 1;
		// If the search element is equal to the current element, we found the element we are looking for
		} else {
			console.log("Current Index: ", currentIndex);
			console.log("Current Element: ", currentElement);
			console.log("-----------");
			return currentIndex;
		}
	}
	return false;
}
// Binary search only works when the array of interest is sorted (alphabetically, numerically)
Binary search only works when the array of interest is sorted (alphabetically, numerically)
Binary search only works when the array of interest is sorted ( alphabetically numerically )
function binarySearch(arr, searchElement) {
	var currentElement;
	var currentIndex;
	var maxIndex = arr.length - 1;
	var minIndex = 0;
	while (minIndex <= maxIndex) {
		currentIndex = Math.floor((minIndex + maxIndex) / 2);
		currentElement = arr[currentIndex];
		console.log("Start Index: ", minIndex);
		console.log("End Index: ", maxIndex);
		console.log("Current Element: ", currentElement);
		if (currentElement < searchElement) {
			minIndex = currentIndex + 1;
		} else if (currentElement > searchElement) {
			maxIndex = currentIndex - 1;
		} else {
			console.log("Current Index: ", currentIndex);
			console.log("Current Element: ", currentElement);
			console.log("-----------");
			return currentIndex;
		}
	}
	return false;
}
function binarySearch ( arr , searchElement ) { var currentElement ; var currentIndex ; var maxIndex = arr . length - 1 ; var minIndex = 0 ; while ( minIndex <= maxIndex ) { currentIndex = Math . floor ( ( minIndex + maxIndex ) / 2 ) ; currentElement = arr [ currentIndex ] ; console . log ( "Start Index: " , minIndex ) ; console . log ( "End Index: " , maxIndex ) ; console . log ( "Current Element: " , currentElement ) ; if ( currentElement < searchElement ) { minIndex = currentIndex + 1 ; } else if ( currentElement > searchElement ) { maxIndex = currentIndex - 1 ; } else { console . log ( "Current Index: " , currentIndex ) ; console . log ( "Current Element: " , currentElement ) ; console . log ( "-----------" ) ; return currentIndex ; } } return false ; }
Binary search only works when the array of interest is sorted (alphabetically, numerically)
Binary search only works when the array of interest is sorted ( alphabetically numerically )
// Set starting values // Get a position near the middle // Get the element at that index // If the search element is greater than the current element, look above this value // If the search element is less than the current element, look below this value // If the search element is equal to the current element, we found the element we are looking for