-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.java
More file actions
58 lines (43 loc) · 913 Bytes
/
Copy pathbinarySearch.java
File metadata and controls
58 lines (43 loc) · 913 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.*;
public class binarySearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]= {23,44,55,67,89,1010,13232};
System.out.println("Enter Element to be binary searched");
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
int end=arr.length;
int start=0;
int count=0;
int temp=n;
while(start<end)
{
int midIndex=(end+start)/2;
// System.out.println(start);
if(arr[midIndex]==n)
{
temp=arr[midIndex];
count+=1;
break;
}
else if(arr[midIndex]<n)
{
start=midIndex+1;
count=0;
}
else
{
end=midIndex-1;
count=0;
}
}
if(count==1)
{
System.out.println(temp +"\t Element is present");
}
else
{
System.out.println("Element not present in the array");
}
}
}