-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathniharika.java
37 lines (33 loc) · 1.02 KB
/
niharika.java
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
//Binary Search
import java.util.Scanner;
import java.util.Arrays;
public class Main
{ public static void main(String[] args)
{ Scanner sc= new Scanner(System.in);
int numberOfElements= sc.nextInt();
int[] arr= new int[numberOfElements];
for(int i=0;i<arrlength;i++)
arr[i]= sc.nextInt();
int searchedElement= sc.nextInt();
binarySearch(arr,searchedElement);
}
private static void binarySearch(int[] arr,searchedElement)
{ Arrrays.sort(arr);
for(int i=0;i<arr.length;i++)
{ int lowestNum=0;
int highestNum=arr.length-1;
while(lowestNum<=highestNum)
{int midNum=(lowestNum+highestNum)/2;
if(searchedElement>arr[midNum])
lowestNum=midNum+1;
else if(searchedElement>arr[midNum])
highestNum=midNum-1;
else
{System.out.println("Number is found");
return;
}
}
}
System.out.println("Number isn't found");
}
}