You are on page 1of 3

2CS503 Design and Analysis of Algorithms

Tutorial 3: Recursive program and sorting

Q1. Predict the output of the following program:


a. b.
On recursion, the value of f(513,2) void myFun(unsigned int n)
{
int f(int n, int r) if(n != 0)
{ {
if(n<=0) myFun(n/2);
return 0; printf("%d", n % 2);
else }
return ( n%r + f(n/r, r) ) }
} int main()
{
myFun(25);
getchar();
return 0;
}
c. d.
int n=1;
#include<stdio.h> void odd()
int fun(int a[],int n) {
{ if(n <= 10)
int x; {
if(n == 1) printf("%d ", n+1);
return a[0]; n++;
else even();
x = fun(a, n-1); }
if(x > a[n-1]) return;
return x; }
else void even()
return a[n-1];
{
} if(n <= 10)
{
int main()
printf("%d ", n-1);
{
n++;
int arr[] = {12, 10, 30, 50, 100}; odd();
printf(" %d ", fun(arr, 5)); }
getchar();
return;
return 0; }
} int main()
{
odd();
}
e. f.
#include <stdio.h> int fun1 (int n) {
int rfunc (int a) static int i = 0;
{ if (n > 0) {
if(a == 0) ++i;
return 0; fun1(n-1);
else }
{ return (i);
printf("%d ",a); }
return rfunc(a-1);
printf("%d ",a); int fun2 (int n) {
} static int i = 0;
} if (n > 0) {
int main() i = i + fun1 (n);
{ fun2(n-1);
rfunc(5); }
return (i);
return 0; }
} fun2(5) is called from main()

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

You might also like