Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BinarySearch {
- public static void main(String[] args) {
- int[] ar = {1, 3, 5, 6, 2};
- System.out.print(binarySearch(ar, 5));
- }
- private static boolean binarySearch(int[] ar, int searchItem) {
- int i = 1;
- int lower = 0;
- int upper = ar.length - 1;
- int mid;
- while (true) {
- mid = (lower + upper) / 2;
- if (ar[mid] == searchItem)
- i++;
- //return true;
- else
- if (lower > upper)
- i++;
- //return false;
- else
- if (searchItem > ar[mid])
- lower = mid + 1;
- else
- upper = mid - 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement