Skip to content

Commit c8a9cf4

Browse files
committed
Added Binary Search
1 parent 5614404 commit c8a9cf4

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)