Last active
March 22, 2018 16:44
-
-
Save alpgul/96cb9098e0db0e1d6c758e7003ddaab3 to your computer and use it in GitHub Desktop.
Binary Search with Javascript
This file contains 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
/*---- | |
Best Case:1 | |
Worst Case:logn | |
Average Case:logn | |
Unsuccessful:logn | |
----- | |
*/ | |
arr=[1,2,3,4,5],val=5; | |
function binary_search(array,value){ | |
let lowerBound = 0; | |
let upperBound = array.length-1; | |
let index; | |
while(lowerBound<=upperBound){ | |
let midPoint = lowerBound +Math.floor(( upperBound - lowerBound ) / 2); | |
if(array[midPoint]==value){ | |
index=midPoint; | |
break;} | |
else{ | |
if(array[midPoint]<value) | |
lowerBound=midPoint+1; | |
else { | |
upperBound=midPoint-1; | |
} | |
} | |
} | |
if(index!==null){ | |
return "bulunan değer sirasi:"+index; | |
} | |
return "değer bulunamadi"; | |
} | |
console.log(binary_search(arr,val)); |
Author
alpgul
commented
Mar 22, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment