-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathLinearSearchRecursive.java
More file actions
33 lines (25 loc) · 884 Bytes
/
LinearSearchRecursive.java
File metadata and controls
33 lines (25 loc) · 884 Bytes
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 LinearSearchRecursive {
public static int linearSearchRecursive(int[] arr, int n, int index, int target) {
// Base case: If index exceeds array bounds, return -1 indicating target is not found
if (index >= n) {
return -1;
}
// If the current element matches the target, return the index
if (arr[index] == target) {
return index;
}
// Recursive case: move to the next index in the array
return linearSearchRecursive(arr, n, index + 1, target);
}
public static void main(String[] args) {
int[] arr = {10, 20, 60, 30, 60, 70, 80, 22};
int n = arr.length;
int target = 70;
int result = linearSearchRecursive(arr, n, 0, target);
if (result >= 0) {
System.out.println("Target element found at index: " + result);
} else {
System.out.println("Target element not found.");
}
}
}