-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExclinearSearch.java
33 lines (29 loc) · 979 Bytes
/
ExclinearSearch.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
public class Tester {
public static int searchElement(int[] arr, int target) {
int iterations = 0;
for (int i = 0; i < arr.length; i++) {
iterations++;
if (arr[i] == target) {
return iterations;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {5, 3, 7, 1, 4, 9, 8};
int target = 4;
int result = searchElement(arr, target);
if (result != -1) {
System.out.println("TargetElement1 found in " + result + " iterations.");
} else {
System.out.println("TargetElement1 not found.");
}
target = 6;
result = searchElement(arr, target);
if (result != -1) {
System.out.println("TargetElement2 found in " + result + " iterations.");
} else {
System.out.println("TargetElement2 not found.");
}
}
}