-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path_12.BinarySearch.java
More file actions
41 lines (39 loc) · 1.05 KB
/
_12.BinarySearch.java
File metadata and controls
41 lines (39 loc) · 1.05 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
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int choice=1,flag=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements :");
int size = sc.nextInt();
int arr[] = new int [size];
System.out.println("Enter the array elements(sorted):");
for(int i=0; i<size; i++)
arr[i]=sc.nextInt();
do{
System.out.print("Enter the element to be searched : ");
int key = sc.nextInt();
int beg=0, end=size-1;
for(int i=0; i<size; i++)
{
int mid = (beg+end)/2;
if(arr[mid]==key)
{
flag=1;
System.out.println("Element Found at position :"+(mid+1));
break;
}
else if(key>arr[mid])
beg = mid+1;
else
end = mid-1;
}
if(flag==0)
System.out.println("Element Not Found !");
System.out.println("*************************************************");
System.out.println("Want to search for another element? (1.Yes/2.No)");
choice = sc.nextInt();
}while(choice==1);
}
}