Skip to content

Commit

Permalink
Added program for Binary Search Using JavaScript (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
shivanisorte authored Oct 1, 2021
1 parent 7420c4e commit c0e80d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions javascript/binary_search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const arr = [1,2,3,4,5,6,7,8,9,20];

const binary_search = (arr, start, end, num) => {
const midele = start + Math.floor((end - start)/2);
if(start <= end){
if(arr[midele] === num){
return "Element found at "+ midele;
}
if(num < arr[midele]){
return binary_search(arr, start, midele-1, num);
}
if(num > arr[midele]){
return binary_search(arr, midele+1, end, num);
}
}
return "Element not found";
};

console.log(binary_search(arr, 0, arr.length-1, 5));
2 changes: 1 addition & 1 deletion javascript/bubbleSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ console.log(bubble_Sort([3, 0, 2, 5, -1, 4, 1]));
/*
Output :-
Sorted Array :-
[ -1, 0, 1, 2, 3, 4, 5]
[ -1, 0, 1, 2, 3, 4, 5]*/

0 comments on commit c0e80d1

Please sign in to comment.