Created
June 22, 2019 07:13
-
-
Save mjarpitanand/0047463ff90ef46f4204ab87b0993efb to your computer and use it in GitHub Desktop.
BinarySearch.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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