-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patharray_binary_int.java
More file actions
55 lines (54 loc) · 1.33 KB
/
array_binary_int.java
File metadata and controls
55 lines (54 loc) · 1.33 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
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Scanner;
public class array_binary_int
{//ascending
int arr[],n,i,ele;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter limit of array");
n=sc.nextInt();
arr=new int[n];
System.out.println("enter "+n+" elements");
for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("enter element to search");
ele=sc.nextInt();
}
void display()
{
for(i=0;i<n;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
void binary()
{
boolean flag=false;
int first=0,last=n-1,mid=0;
while(first<=last)
{
mid=(first+last)/2;
if(arr[mid]<ele)
first=mid+1;
else if(arr[mid]>ele)
last=mid-1;
else
{
flag=true;
break;
}
}
if(flag==true)
System.out.println("found at index"+(mid+1));
else
System.out.println("not found");
}
public static void main(String args[])
{
array_binary_int obj=new array_binary_int();
obj.input();
obj.display();
obj.binary();
}
}