Last active
April 15, 2016 05:11
-
-
Save ba0f3/3dcb36678194edaa83fff71b66aab456 to your computer and use it in GitHub Desktop.
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 doSearch = function(array, targetValue) { | |
var min = 0; | |
var count = 0; | |
var max = array.length - 1; | |
var guess; | |
while(min <= max) { | |
guess = Math.floor((max + min)/2); | |
if(array[guess] === targetValue) { | |
count++; | |
println(guess); | |
return guess; | |
} | |
//again | |
else if (targetValue < array[guess]){ | |
max = guess - 1; | |
} | |
else { | |
min = guess + 1; | |
} | |
} | |
return count; | |
}; | |
var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, | |
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]; | |
var result = doSearch(primes, 73); | |
println("Found prime at index " + result); | |
Program.assertEqual(doSearch(primes, 73), 20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment