Skip to content

Commit 1822ea3

Browse files
authored
Merge pull request #362 from pth55/feature/linear-search-recursive
added LinearSearchRecursive.java and updated README.md
2 parents 704ad9e + a777b8f commit 1822ea3

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,8 +1469,8 @@ In order to achieve greater coverage and encourage more people to contribute to
14691469
</a>
14701470
</td>
14711471
<td> <!-- Java -->
1472-
<a href="./CONTRIBUTING.md">
1473-
<img align="center" height="25" src="./logos/github.svg" />
1472+
<a href="./src/java/LinearSearchRecursive.java">
1473+
<img align="center" height="25" src="./logos/java.svg" />
14741474
</a>
14751475
</td>
14761476
<td> <!-- Python -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class LinearSearchRecursive {
2+
3+
public static int linearSearchRecursive(int[] arr, int n, int index, int target) {
4+
// Base case: If index exceeds array bounds, return -1 indicating target is not found
5+
if (index >= n) {
6+
return -1;
7+
}
8+
9+
// If the current element matches the target, return the index
10+
if (arr[index] == target) {
11+
return index;
12+
}
13+
14+
// Recursive case: move to the next index in the array
15+
return linearSearchRecursive(arr, n, index + 1, target);
16+
}
17+
18+
public static void main(String[] args) {
19+
int[] arr = {10, 20, 60, 30, 60, 70, 80, 22};
20+
21+
int n = arr.length;
22+
23+
int target = 70;
24+
25+
int result = linearSearchRecursive(arr, n, 0, target);
26+
27+
if (result >= 0) {
28+
System.out.println("Target element found at index: " + result);
29+
} else {
30+
System.out.println("Target element not found.");
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)