-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcBinarySearch.java
45 lines (38 loc) · 1.34 KB
/
ExcBinarySearch.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
public class Tester {
public static int searchElement(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;
int iterations = 0;
while (left <= right) {
iterations++;
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
System.out.println("Element found in " + iterations + " iterations.");
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
System.out.println("Element not found after " + iterations + " iterations.");
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 3, 4, 5, 7, 8, 9};
int target = 4;
int result = searchElement(arr, target);
if (result != -1) {
System.out.println("TargetElement1 found at index " + result + ".");
} else {
System.out.println("TargetElement1 not found.");
}
target = 6;
result = searchElement(arr, target);
if (result != -1) {
System.out.println("TargetElement2 found at index " + result + ".");
} else {
System.out.println("TargetElement2 not found.");
}
}
}