Skip to content

Instantly share code, notes, and snippets.

@FeepingCreature
Created May 29, 2012 16:16
Show Gist options
  • Save FeepingCreature/2829302 to your computer and use it in GitHub Desktop.
Save FeepingCreature/2829302 to your computer and use it in GitHub Desktop.
bintest
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