Skip to content

Instantly share code, notes, and snippets.

@ecaepsey
Created May 10, 2019 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecaepsey/260261fb92cd74d606ff8c401141eeb4 to your computer and use it in GitHub Desktop.
Save ecaepsey/260261fb92cd74d606ff8c401141eeb4 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/xagihiy
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var binarySearch = function(array, value) {
var guess,
min = 0,
max = array.length - 1;
while(min <= max){
guess = Math.floor((min + max) /2);
if(array[guess] === value)
return guess;
else if(array[guess] < value)
min = guess + 1;
else
max = guess - 1;
}
return -1;
}
console.log(binarySearch([1,2,3,4, 0,1,8, 9], 9))
</script>
<script id="jsbin-source-javascript" type="text/javascript">var binarySearch = function(array, value) {
var guess,
min = 0,
max = array.length - 1;
while(min <= max){
guess = Math.floor((min + max) /2);
if(array[guess] === value)
return guess;
else if(array[guess] < value)
min = guess + 1;
else
max = guess - 1;
}
return -1;
}
console.log(binarySearch([1,2,3,4, 0,1,8, 9], 9))</script></body>
</html>
var binarySearch = function(array, value) {
var guess,
min = 0,
max = array.length - 1;
while(min <= max){
guess = Math.floor((min + max) /2);
if(array[guess] === value)
return guess;
else if(array[guess] < value)
min = guess + 1;
else
max = guess - 1;
}
return -1;
}
console.log(binarySearch([1,2,3,4, 0,1,8, 9], 9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment