From 6128f1b6a8be45d5e0674d7e7d22b554a8ef6cf4 Mon Sep 17 00:00:00 2001 From: Sreekar Movva Date: Sat, 25 Jan 2025 11:56:22 +0530 Subject: [PATCH 1/3] Add recursive linear search implementation in Java --- src/java/LinearSearchRecursive.java | 49 +++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/java/LinearSearchRecursive.java diff --git a/src/java/LinearSearchRecursive.java b/src/java/LinearSearchRecursive.java new file mode 100644 index 00000000..bc8f0db1 --- /dev/null +++ b/src/java/LinearSearchRecursive.java @@ -0,0 +1,49 @@ +import java.util.Scanner; + +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) { + // Create a scanner object to read user input + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter the size of the array: "); + int n = scanner.nextInt(); + + int[] arr = new int[n]; + + System.out.println("Enter the elements of the array: "); + for (int i = 0; i < n; i++) { + arr[i] = scanner.nextInt(); + } + + System.out.print("Enter the element you need to search for in the array: "); + int target = scanner.nextInt(); + + // Call the recursive linear search function + 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."); + } + + // Close the scanner to avoid memory leaks + scanner.close(); + } +} From 2ff4950c0e023d53d77a7731e9c9859d6e280842 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Bandaru Date: Sat, 25 Jan 2025 12:25:32 +0530 Subject: [PATCH 2/3] Add recursive linear search implementation in Java --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 48882c8c..311b63c1 100644 --- a/README.md +++ b/README.md @@ -1469,7 +1469,7 @@ In order to achieve greater coverage and encourage more people to contribute to - + From fde00535daac5e5341490fbb8f46a04adb4c7a04 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Bandaru Date: Sat, 25 Jan 2025 12:31:39 +0530 Subject: [PATCH 3/3] Add recursive linear search implementation in Java --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 311b63c1..fd975252 100644 --- a/README.md +++ b/README.md @@ -1470,7 +1470,7 @@ In order to achieve greater coverage and encourage more people to contribute to - +