Skip to content

update-binarysearch #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Array/Binary_Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ let RecursiveFunction = function (arr, x, start, end) {
if (start > end) return false;


let mid=Math.floor((start + end)/2);
let mid=Math.floor(start + (end - start)/2);
//rather than using mid = (start + mid)/2 using the above as in some cases (start + end) can be out of the range


if (arr[mid]===x) return true;
Expand Down
2 changes: 1 addition & 1 deletion Search/binarySearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function binarySearch(arr = [], key) {
var left = 0,
right = arr.length - 1; // left index and right index of the array
while (left <= right) {
var middle = Math.floor((right + left) / 2);
var middle = Math.floor( left +(right - left) / 2);
if (arr[middle] == key) return middle;
else if (key > arr[middle]) {
left = middle + 1;
Expand Down