Created
May 29, 2012 16:16
-
-
Save FeepingCreature/2829302 to your computer and use it in GitHub Desktop.
bintest
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
module bintest; | |
// assume array is sorted from small to big | |
int findIndexOfOne(int[] array, int value) { | |
if (!array.length) raise new Error "$value not found during binsearch"; | |
auto test-idx = array.length / 2; | |
if (array[test-idx] == value) return test-idx; | |
if (array[test-idx] < value) // everything before test-idx is also smaller | |
return findIndexOfOne(array[test-idx+1 .. $], value) + test-idx+1; | |
else // everything after test-idx is also larger | |
return findIndexOfOne(array[0..test-idx], value); | |
} | |
void main() { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment