Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.random;
- import std.stdio;
- import std.functional;
- int[1000] a;
- bool bsearch(T, alias cmp = "a < b")(T[] a, T x)
- {
- int l=0, r=a.length;
- while (l+1 < r)
- {
- int m = (l+r)/2;
- int am = a[m];
- if (binaryFun!(cmp)(x, am))
- r = m;
- else
- l = m;
- }
- return a[0] == x;
- }
- void main()
- {
- foreach (ref n; a)
- n = rand()%1000;
- a.sort;
- foreach (iter; 0..100_000_000)
- bsearch(a[], cast(int)(rand()%1000));
- }
Add Comment
Please, Sign In to add comment