-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.java
53 lines (40 loc) · 1.38 KB
/
BinarySearch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package Arrays;
import java.util.Scanner;
public class BinarySearch {
public static void main (String[]args) {
Scanner sc = new Scanner(System.in);
String name[]= new String[20];
double height[]= new double[20];
for (int i=0;i<20;i++){
System.out.println("Please enter Name ");
name[i]=sc.nextLine();
System.out.println("Please enter Height");
height[i]=sc.nextDouble();
sc.nextLine(); //
}
System.out.println("Please enter friend's height");
double search =sc.nextDouble();
int lb,ub,mid=0;
ub=height.length-1;
lb=0;
boolean check=false; //flag
while (lb<=ub){
mid = (ub+lb)/2;
if (search<height[mid])
lb = mid + 1;
else if (search==height[mid]){
System.out.println("Friend's name and height found at "+(mid+1));
System.out.println(name[mid]);
System.out.println(height[mid]);
check=true;
break;
}
else
ub = mid - 1;
}
if (check!=true)
System.out.println("Friend's name and height not found");
sc.close();
System.exit(0);
}
}