Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mjarpitanand/0047463ff90ef46f4204ab87b0993efb to your computer and use it in GitHub Desktop.
Save mjarpitanand/0047463ff90ef46f4204ab87b0993efb to your computer and use it in GitHub Desktop.
BinarySearch.js
let arr = [2,45,67,89,100,103];
let key = 80;
function binary_serch(arr , key){
let low = 0;
let high = arr.length - 1;
while(low <= high){
let mid = Math.floor((low+high)/2);
console.log(low, mid, high)
if(arr[mid] > key){
high = mid - 1;
}else if(arr[mid] < key){
low = mid + 1;
}else{
console.log('Element Found');
return;
}
}
console.log('No element Found');
return key+" is not found!";
}
console.log(binary_serch(arr , key));
let arr = [2,45,67,89,100,103];
let key = 80;
function binary_serch(arr , key){
let low = 0;
let high = arr.length - 1;
while(low <= high){
let mid = Math.floor((low+high)/2);
console.log(low, mid, high)
if(arr[mid] > key){
high = mid - 1;
}else if(arr[mid] < key){
low = mid + 1;
}else{
console.log('Element Found');
return;
}
}
console.log('No element Found');
return key+" is not found!";
}
console.log(binary_serch(arr , key));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment