-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram41.java
More file actions
42 lines (30 loc) · 1.02 KB
/
Copy pathprogram41.java
File metadata and controls
42 lines (30 loc) · 1.02 KB
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
// Binary Search Algorithm in DS
import java.util.Scanner;
public class program41 {
public static void main(String[] args) {
int []arr = {10,20,30,40,50,60,70,80,90,100};
System.out.println("Array Elements Are : ");
for(int i =0; i<arr.length; i++){
System.out.print(arr[i]+" ");
}
int low =0;
int high = arr.length-1;
int mid;
System.out.println();
Scanner sc = new Scanner(System.in);
System.out.print("Eneter Element to Search : ");
int item = sc.nextInt();
while(low<=high){
mid = (low+high)/2;
if(arr[mid] == item){
System.out.print("Element Found At Index : " + mid);
return;
} else if(arr[mid] < item){
low = mid+1;
}else{
high = mid-1;
}
} System.out.println("Element Not Found !!!!!");
sc.close();
}
}