forked from anku580/Java-Algorithms
-
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) · 760 Bytes
/
BinarySearch.java
File metadata and controls
33 lines (32 loc) · 760 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
package Searching;
import java.util.Arrays;
import java.util.Scanner;
public class BinarySearch {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=sc.nextInt();//size of array
int array[]=new int[n];//array declaration
for(int i=0;i<n;i++)array[i]=sc.nextInt();
Arrays.sort(array);
int search=sc.nextInt();
int ui=n-1;
int li=0;
boolean found=false;
while(true) {
int curin=(ui+li)/2;
if(search==array[curin]) {
found=true;
break;
}
else if(li>ui)break;
else if(search>array[curin])
li=curin+1;
else
ui=curin-1;
}
if(found)System.out.println("Yes");
else
System.out.println("No");
}
}