forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BinarySearch.java
34 lines (31 loc) · 1.09 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
public class BinarySearch {
static int binarySearch(int[] arr, int searchElement) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == searchElement) { // Element found
return mid;
}
if (arr[mid] < searchElement) { // Look in right half
left = mid + 1;
} else { // Look in left half
right = mid - 1;
}
}
return -1; // Element not found
}
public static void main(String[] args) {
int[] arr = new int[] {1, 5, 35, 112, 258, 324};
int[] searchArr = new int[] {1, 35, 112, 324, 67};
int pos;
for (int i = 0; i < searchArr.length; i++) {
pos = binarySearch(arr, searchArr[i]); //search key and get poistion
if (pos >= 0) {
System.out.println(searchArr[i] + "-> found at index : " + pos);
} else {
System.out.println(searchArr[i] + "-> not found");
}
}
}
}