Practice Questions for Recursion | Set 1
Explain the functionality of following functions.
Question 1
int fun1(int x, int y) { if(x == 0) return y; else return fun1(x - 1, x + y); } |
chevron_right
filter_none
Answer: The function fun() calculates and returns ((1 + 2 … + x-1 + x) +y) which is x(x+1)/2 + y. For example if x is 5 and y is 2, then fun should return 15 + 2 = 17.
Question 2
void fun2(int arr[], int start_index, int end_index) { if(start_index >= end_index) return; int min_index; int temp; /* Assume that minIndex() returns index of minimum value in array arr[start_index...end_index] */ min_index = minIndex(arr, start_index, end_index); temp = arr[start_index]; arr[start_index] = arr[min_index]; arr[min_index] = temp; fun2(arr, start_index + 1, end_index); } |
chevron_right
filter_none
Answer: The function fun2() is a recursive implementation of Selection Sort.
Please write comments if you find any of the answers/codes incorrect, or you want to share more information about the topics discussed above.
Recommended Posts:
- Practice Questions for Recursion | Set 3
- Practice Questions for Recursion | Set 6
- Practice Questions for Recursion | Set 2
- Practice Questions for Recursion | Set 7
- Practice Questions for Recursion | Set 5
- Practice Questions for Recursion | Set 4
- Practice questions for Linked List and Recursion
- Recursive Practice Problems with Solutions
- Recursion
- Find the value of ln(N!) using Recursion
- Sum of the series 1^1 + 2^2 + 3^3 + ..... + n^n using recursion
- Tail Recursion
- Product of 2 numbers using recursion | Set 2
- Reversing a queue using recursion
- Generating all possible Subsequences using Recursion


