0% found this document useful (0 votes)
315 views2 pages

What Is Recurrence For Worst Case of QuickSort and What Is The Time Complexity in Worst Case

The document discusses recurrence relations and time complexities for various algorithms: (A) Quicksort has a recurrence of T(n) = T(n-1) + O(n) and time complexity of O(n^2) in the worst case. (B) The function fun(int n) has a time complexity of O(n) as its innermost statement is executed n + n/2 + n/4 + ... 1 ≈ n times. (C) The recurrence for the optimal time of the Tower of Hanoi problem is T(n) = 2T(n–1) + 1.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
315 views2 pages

What Is Recurrence For Worst Case of QuickSort and What Is The Time Complexity in Worst Case

The document discusses recurrence relations and time complexities for various algorithms: (A) Quicksort has a recurrence of T(n) = T(n-1) + O(n) and time complexity of O(n^2) in the worst case. (B) The function fun(int n) has a time complexity of O(n) as its innermost statement is executed n + n/2 + n/4 + ... 1 ≈ n times. (C) The recurrence for the optimal time of the Tower of Hanoi problem is T(n) = 2T(n–1) + 1.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

What is recurrence for worst case of QuickSort and what is the time complexity in Worst

case?
(A) Recurrence is T(n) = T(n-2) + O(n) and time complexity is O(n^2)
(B) Recurrence is T(n) = T(n-1) + O(n) and time complexity is O(n^2)
(C) Recurrence is T(n) = 2T(n/2) + O(n) and time complexity is O(nLogn)
(D) Recurrence is T(n) = T(n/10) + T(9n/10) + O(n) and time complexity is O(nLogn)

Answer: (B)

What is time complexity of fun()?

filter_none
edit
play_arrow
brightness_4
int fun(int n)
{
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count += 1;
return count;
}
(A) O(n^2)
(B) O(nLogn)
(C) O(n)
(D) O(nLognLogn)

Answer: (C)

Explanation: For a input integer n, the innermost statement of fun() is executed


following times.
n + n/2 + n/4 + … 1
So time complexity T(n) can be written as
T(n) = O(n + n/2 + n/4 + … 1) = O(n)
The value of count is also n + n/2 + n/4 + .. + 1
he recurrence relation capturing the optimal time of the Tower of Hanoi problem with
n discs is. (GATE CS 2012)
(A) T(n) = 2T(n – 2) + 2
(B) T(n) = 2T(n – 1) + n
(C) T(n) = 2T(n/2) + 1
(D) T(n) = 2T(n – 1) + 1
Answer: (D)

Which of the following algorithms is NOT a divide & conquer algorithm by nature?
(A) Euclidean algorithm to compute the greatest common divisor
(B) Heap Sort
(C) Cooley-Tukey fast Fourier transform
(D) Quick Sort

Answer: (B)

Consider the following C program

filter_none
edit
play_arrow
brightness_4
int main()
{
int x, y, m, n;
scanf ("%d %d", &x, &y);
/* x > 0 and y > 0 */
m = x; n = y;
while (m != n)
{
if(m>n)
m = m - n;
else
n = n - m;
}
printf("%d", n);
}
What does the program compute? (GATE CS 2004)
(A) x + y using repeated subtraction
(B) x mod y using repeated subtraction
(C) the greatest common divisor of x and y
(D) the least common multiple of x and y

Answer: (C)

You might also like