-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearch.java
62 lines (52 loc) · 1.24 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.Scanner;
class BinarySearch {
public static void main(String[] args) {
int a[]; // only a array reference
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array:");
n = sc.nextInt();
a = new int[n];
// Take array input
System.out.println("Enter values into array:");
for (int i = 0; i < n; ++i)
{
System.out.printf("Enter %d index elem:", i);
a[i] = sc.nextInt();
}
System.out.print("Enter an element to search: ");
int key = sc.nextInt();
int position = binarySearch(a, a.length, key);
if (position == -1)
{
System.out.println("Key not found :(");
}
else{
System.out.println("Key found at " + position + "position");
}
}
public static int binarySearch(int a[], int n, int key)
{
int lb, ub, mid;
lb = 0;
ub = n - 1;
while (lb <= ub)
{
mid = (lb + ub) / 2;
if (a[mid] == key)
{
return key;
}
else {
if (a[mid] < key)
{
lb = mid + 1;
}
else {
ub = mid - 1;
}
}
}
return -1;
}
}