Created
September 15, 2018 02:40
-
-
Save yurychika/fe427d1f2bf24efe095a44ef970529d3 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/kaqohiz
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
<!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 a = [1,2,3,4,5,6,7,8,9]; | |
function binarySearch(ary, num) { | |
function bs(start, end) { | |
var mid = Math.floor((start + end) / 2) | |
console.log('mid is', mid) | |
if (start === mid) return num === ary[mid] ? mid : -1; | |
if (ary[mid] > num) return bs(start, mid); | |
if (ary[mid] === num) return mid; | |
if (ary[mid] < num) return bs(mid + 1, end); | |
}; | |
return bs(0, ary.length - 1); | |
} | |
console.log(binarySearch(a, 8)) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var a = [1,2,3,4,5,6,7,8,9]; | |
function binarySearch(ary, num) { | |
function bs(start, end) { | |
var mid = Math.floor((start + end) / 2) | |
console.log('mid is', mid) | |
if (start === mid) return num === ary[mid] ? mid : -1; | |
if (ary[mid] > num) return bs(start, mid); | |
if (ary[mid] === num) return mid; | |
if (ary[mid] < num) return bs(mid + 1, end); | |
}; | |
return bs(0, ary.length - 1); | |
} | |
console.log(binarySearch(a, 8))</script></body> | |
</html> |
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
var a = [1,2,3,4,5,6,7,8,9]; | |
function binarySearch(ary, num) { | |
function bs(start, end) { | |
var mid = Math.floor((start + end) / 2) | |
console.log('mid is', mid) | |
if (start === mid) return num === ary[mid] ? mid : -1; | |
if (ary[mid] > num) return bs(start, mid); | |
if (ary[mid] === num) return mid; | |
if (ary[mid] < num) return bs(mid + 1, end); | |
}; | |
return bs(0, ary.length - 1); | |
} | |
console.log(binarySearch(a, 8)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment