Advertisement
thienlang

Binary Search

Oct 4th, 2013
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.30 KB | None | 0 0
  1. //thuat toan tim kiem nhi phan
  2. int Binary(int* a, int n, int x)
  3. {
  4.     int left = 0;
  5.     int right = n-1;
  6.     int mid;
  7.     while ( left <= right)
  8.     {
  9.         mid =( left + right ) / 2;
  10.         if ( x < a[mid] )
  11.             right = mid - 1;
  12.         else if ( x > a[mid] )
  13.             left = mid + 1;
  14.         else
  15.             return mid;
  16.     }
  17.     return -1;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement