Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.random;
- import std.stdio;
- int[1000] a;
- bool bsearch(T)(T[] a, T x)
- {
- int l=0, r=a.length;
- while (l+1 < r)
- {
- int m = (l+r)/2;
- int am = a[m];
- if (am == x)
- return true;
- else
- if (am > x)
- r = m;
- else
- l = m+1;
- }
- 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