Guest User

Untitled

a guest
Jun 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.39 KB | None | 0 0
  1. import std.random;
  2. import std.stdio;
  3.  
  4. int[1000] a;
  5.  
  6. bool bsearch(T)(T[] a, T x)
  7. {
  8. int l=0, r=a.length;
  9. while (l+1 < r)
  10. {
  11. int m = (l+r)/2;
  12. int am = a[m];
  13. if (am == x)
  14. return true;
  15. else
  16. if (am > x)
  17. r = m;
  18. else
  19. l = m+1;
  20. }
  21. return a[0] == x;
  22. }
  23.  
  24. void main()
  25. {
  26. foreach (ref n; a)
  27. n = rand()%1000;
  28. a.sort;
  29.  
  30. foreach (iter; 0..100_000_000)
  31. bsearch(a[], cast(int)(rand()%1000));
  32. }
Add Comment
Please, Sign In to add comment