Skip to content

Commit 7828b75

Browse files
authored
Merge pull request vJechsmayr#96 from Jaideep12/master
Added Binary Search Algorithm
2 parents 7901007 + c8a9cf4 commit 7828b75

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

‎SearchAlgorithms/BinarySearch.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function binarySearch(sortedArray, elementToFind) {
2+
var lowIndex = 0;
3+
var highIndex = sortedArray.length - 1;
4+
while (lowIndex <= highIndex) {
5+
var midIndex = Math.floor((lowIndex + highIndex) / 2);
6+
if (sortedArray[midIndex] == elementToFind) {
7+
return midIndex;
8+
} else if (sortedArray[midIndex] < elementToFind) {
9+
lowIndex = midIndex + 1;
10+
} else {
11+
highIndex = midIndex - 1;
12+
}
13+
} return null;
14+
}

0 commit comments

Comments
 (0)