-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.java
46 lines (41 loc) · 1.21 KB
/
BinarySearch.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
38
39
40
41
42
43
44
45
46
package Java_DSA.Searching;
import java.util.*;
public class BinarySearch
{
public static int search(int[] arr, int n, int element){
int first = 0;
int last = n-1;
while(first <= last) {
int mid = (first + last)/2;
if (element < arr[mid]) {
last = mid - 1;
}
else if(element > arr[mid]){
first = mid + 1;
}
else {
return mid;
}
}
return -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no of elements : ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements : ");
for(int i = 0;i < n;i++){
arr[i] = sc.nextInt();
}
System.out.println("Enter the searching element : ");
int searchvalue = sc.nextInt();
int result = search(arr,n,searchvalue);
if(result == -1){
System.out.println("Element is not present.");
}
else{
System.out.println("Element is present at index " + result);
}
}
}