We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1aaf0db commit 828a61dCopy full SHA for 828a61d
searching/BinarySearch.java
@@ -0,0 +1,45 @@
1
+package pep5_2Jan;
2
+
3
+import java.util.Scanner;
4
5
+public class BinarySearch {
6
7
+ public static void main(String[] args)
8
+ {
9
+ Scanner scn=new Scanner(System.in);
10
+ int N=scn.nextInt();
11
+ int[] a=new int[N];
12
+ boolean flag=false;
13
+ for(int i=0;i<N;i++)
14
15
+ a[i]=scn.nextInt();
16
+ }
17
+ int low=0,high=(a.length)-1;
18
+ int mid;
19
20
+ int n=scn.nextInt();
21
+ while(high>=low)
22
23
+ mid=(high+low)/2;
24
+ if (a[mid]>n)
25
26
+ high=mid-1;
27
28
+ else if (a[mid]<n)
29
30
+ low=mid+1;
31
32
+ else if (a[mid]==n)
33
+ {System.out.println(mid);
34
+ flag=true;
35
+ break;}
36
37
38
+ if (flag==false)
39
40
+ System.out.println(-1);
41
42
43
44
45
+}
0 commit comments