forked from vedant1771/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysearch.java
More file actions
27 lines (24 loc) · 809 Bytes
/
binarysearch.java
File metadata and controls
27 lines (24 loc) · 809 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
// Java program to demonstrate working of Arrays.
// binarySearch() in a sorted array.
import java.util.Arrays;
public class GFG {
public static void main(String[] args)
{
int arr[] = { 10, 20, 15, 22, 35 };
Arrays.sort(arr);
int key = 22;
int res = Arrays.binarySearch(arr, key);
if (res >= 0)
System.out.println(key + " found at index = "
+ res);
else
System.out.println(key + " Not found");
key = 40;
res = Arrays.binarySearch(arr, key);
if (res >= 0)
System.out.println(key + " found at index = "
+ res);
else
System.out.println(key + " Not found");
}
}