fork(1) download
  1. using System;
  2.  
  3. public class Test
  4. {
  5.  
  6. public static int Bsearch(int[] m, int left, int right, int item)
  7. {
  8. if (left > right)
  9. return -1;
  10. int found = left + (right - left) / 2;
  11. if (m[found] > item)
  12. return Bsearch(m, left, found - 1, item);
  13. else if (m[found] < item)
  14. return Bsearch(m, found + 1, right, item);
  15. else
  16. return m[found];
  17. }
  18.  
  19. public static void Main()
  20. {
  21. int[] massive = new int[20];
  22. for (int i = 0; i<massive.Length; i++)
  23. {
  24. massive[i] = i;
  25. }
  26. int result = Bsearch(massive, 0, massive.Length - 1, -2);
  27. Console.WriteLine("{0} has found", result);
  28. result = Bsearch(massive, 0, massive.Length - 1, 3);
  29. Console.WriteLine("{0} has found", result);
  30. result = Bsearch(massive, 0, massive.Length - 1, 30);
  31. Console.WriteLine("{0} has found", result);
  32. Console.ReadKey();
  33.  
  34. }
  35. }
Success #stdin #stdout 0.02s 14860KB
stdin
Standard input is empty
stdout
-1 has found
3 has found
-1 has found