Modify Fig 192 to use recursive method recursiveLinearSearch
Modify Fig. 19.2 to use recursive method recursiveLinearSearch to perform a linear search of the array. The method should received the search key and the starting index as arguments. If the search key is found, return its index in the array; otherwise return -1. Each call to the recursive method should check one index in the array. Java How To Program (Early Objects), 10th Edition pg. 813
Solution
Hi, Please find my code.
Please let me know in case of any issue.
public class RecursiveLinearSearch {
public static int linearSearchRecursive(int[] input, int key,int index) {
// base case
if (index == input.length-1) {
return -1;
}
// if key found
if (input[index] == key) {
return index;
}
else
return linearSearchRecursive(input,key,++index); // recursive call
}
public static void main(String[] args) {
int[] a = {1,7,4,9,12,32,33,22,67,5,9};
System.out.println(\"Index of 6: \"+linearSearchRecursive(a, 33, 0));
}
}

