Recursive Linear Search
Given the array {3, 7, 9, 5, 9}, what index is returned by findPosition when searching for the value 9 starting at index 0?
public int findPosition(int nums[], int key, int currentIndex) {
if(currentIndex >= nums.length)
return -1;
if(nums[currentIndex] == key)
return currentIndex;
return findPosition(nums, key, currentIndex + 1);
}
// In main:
// int[] players = {3, 7, 9, 5, 9};
// System.out.println(findPosition(players, 9, 0));
A
0
B
2
C
-1
D
4
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE