fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int BinarySearch(int A[],int p,int r,int value)
  5. {
  6. int q = (p+r)/2;
  7.  
  8. if(A[q]==value)
  9. {
  10. return q; //value found
  11. }
  12. if(p==q)
  13. {
  14. return 0; //not found
  15. }
  16. if(A[q]>value)
  17. {
  18. return BinarySearch(A,p,q,value);
  19. }
  20. else
  21. {
  22. return BinarySearch(A,q+1,r,value);
  23. }
  24. }
  25.  
  26. int main() {
  27. int x[] = {1,2,3,4,5,120, 140};
  28. int pos = BinarySearch(x, 0, 7, 140);
  29. printf("%d\n", pos);
  30. return 0;
  31. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
6