Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1469,8 +1469,8 @@ In order to achieve greater coverage and encourage more people to contribute to
</a>
</td>
<td> <!-- Java -->
<a href="./CONTRIBUTING.md">
<img align="center" height="25" src="./logos/github.svg" />
<a href="./src/java/LinearSearchRecursive.java">
<img align="center" height="25" src="./logos/java.svg" />
</a>
</td>
<td> <!-- Python -->
Expand Down
33 changes: 33 additions & 0 deletions src/java/LinearSearchRecursive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.");
}
}
}