-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysearch.java
More file actions
33 lines (32 loc) · 968 Bytes
/
binarysearch.java
File metadata and controls
33 lines (32 loc) · 968 Bytes
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
import java.util.Scanner;
public class binarysearch {
public static void main(String[] args) {
int limit,flag=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the limit:-");
limit=sc.nextInt();
int [] arr=new int[limit];
for (int i = 0; i < limit; i++) {
arr[i]=sc.nextInt();
}
int start=0,end=arr.length-1,element;
System.out.println("Enter the element to search");
element=sc.nextInt();
while (start<=end) {
int mid=start+(end-start)/2;
if(arr[mid]==element){
System.out.println("element found at index "+ mid);
flag=1;
break;
}else if(element > arr[mid]){
start=mid+1;
}else{
end=mid-1;
}
}
if (flag==0) {
System.out.println("Not found");
}
sc.close();
}
}