Skip to content

Instantly share code, notes, and snippets.

@GrooveStomp
Created March 20, 2013 19:27
Show Gist options
  • Select an option

  • Save GrooveStomp/5207668 to your computer and use it in GitHub Desktop.

Select an option

Save GrooveStomp/5207668 to your computer and use it in GitHub Desktop.
D Language array testing/tutorial code from here: https://www.informit.com/articles/article.aspx?p=1381876&seqNum=4
bool binarySearch(T)(T[] input, T value) {
while (!input.empty) {
auto i = input.length / 2;
auto mid = input[i];
if (mid > value) input = input[0 .. i];
else if (mid < value) input = input[i + 1 .. $];
else return true;
}
return false;
}
unittest {
assert(binarySearch([ 1, 3, 6, 7, 9, 15 ], 6));
assert(!binarySearch([ 1, 3, 6, 7, 9, 15 ], 5));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment