Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BinarySearch {
- public static void main (String args[]) {
- int values[] = {11, 12, 15, 16, 112, 118, 123, 145};
- int target = 15;
- int min = 0;
- int high = 7;
- boolean found = false;
- int answer = 0;
- int mid = 0;
- while ((found==false)&&(min<=high)) {
- mid = ((min+high)/2);
- if (values[mid]==target) {
- found = true;
- answer = min;
- }
- else if (target>values[mid]) {
- min = mid+1;
- }
- else {
- high = mid-1;
- }
- }
- if (found == true) {
- System.out.println(target + " found at array index " + answer + ".");
- }
- else {
- System.out.println(target + " was not found.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement