The array is as follows: 1,2,3,6,8,10. Given that the number 17 is to be…
2023
The array is as follows: 1,2,3,6,8,10. Given that the number 17 is to be searched. At which call it tells that there’s no such element? (By using linear search(recursive) algorithm)
- A.
7th call
- B.
9th call
- C.
17th call
- D.
The function calls itself infinite number of times
Attempted by 618 students.
Show answer & explanation
Correct answer: A
Answer: a
Explanation: The function calls itself till the element is found. But at the 7th call it terminates as goes outside the array.
The recursive linear search step by step.
Array: [1, 2, 3, 6, 8, 10]
Element to search: 17
How recursive linear search works:
At each recursive call:
Check current element
If not equal → move to next index
Stop when index reaches array size (base case)
Step-by-step calls:
Call 1 → check 1 → not 17
Call 2 → check 2 → not 17
Call 3 → check 3 → not 17
Call 4 → check 6 → not 17
Call 5 → check 8 → not 17
Call 6 → check 10 → not 17
Call 7 → index = 6 (out of bounds) → base case reached
Final Answer:
7th call
NOTE: For an array of size n, unsuccessful recursive linear search takes n + 1 calls.