Created
March 20, 2013 19:27
-
-
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
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
| 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