Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int BinarySearch(int a[], int length, int key)
- {
- int left = 0;
- int right = length - 1;
- int middle;
- do
- {
- middle = (left + right) / 2;
- if (a[middle] == key)
- {
- return middle;
- }
- else if (a[middle] < key)
- {
- left = middle + 1;
- }
- else
- {
- right = middle - 1;
- }
- }
- while (left <= right);
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment