Professional Documents
Culture Documents
g. h.
int f(int n) void foo (int n, int sum) {
{ int k = 0, j = 0;
static int r = 0; if(n==0) return;
if(n <= 0) return 1; k=n%10; j = n /10;
if(n>3) sum = sum + k;
{ foo(j, sum);
r = n; printf("%d",k);
return f(n-2)+2; }
} int main () {
return f(n-1)+r; int a = 2048, sum = 0;
} foo(a, sum);
What is the value of f(5)? printf("%d\n", sum);
}
i. J.
int f(int n) main( )
{ {
static int i = 1; int x, y, m, n;
if(n>=5) return n; scanf("%d %d", &x, &y);
n = n+1; /* Assume x > 0 and y > 0 */
i++; m=x; n=y;
return f(n); while(m!=n)
} {
The value returned by f(1) is if(m>n)
m=m-n;
else
n=n-m;
}
printf("%d", n);
}
The program computes
Q2. How do you reverse a link list using recursion?
Q3. How to implement bubble sort using recursion?
Q4. Suppose we need to sort an array of eight integers using the quicksort algorithm,
and we just finished the partitioning with the array: {2, 5, 1, 7, 9, 12, 11, 10}?
a) The pivot could be either the seven or the 9.
b) The pivot could be the 7, and it is not the 9.
c) The pivot is not the 7, and it could be the 9.
d) Neither the seven nor the 9 is the pivot.
Q5. Consider a situation where swapping operation is very costly. Which of the given
sorting algorithms should be preferred so that the number of swap operations is
minimum in general?
a) Heap Sort
b) Selection Sort
c) Insertion Sort
d) Merge Sort
Q6. Suppose we sort an array of eight integers using heapsort, and we have just
finished some heapify (either minheapify or maxheapify) operations. The array
looks like this: {16 14 15 10 12 27 28} How many heapify operations have been
performed on the root of the heap?
a) 1
b) 2
c) 3 or 4
d) 5 or 6
Q7. Which of the below-sorting algorithms in its typical implementation gives the best
performance when applied on an array that is sorted or almost sorted (maximum 1
or 2 elements are misplaced)?
a) Insertion Sort
b) Merge Sort
c) Quick Sort
d) Heap Sort