GATE 2007
184 questions
MCQ 1841-mark 184
Questions 91–95 of 184
Which of the following sorting algorithms has the lowest worst-case complexity?
Answer: (A)
Worst-case time of each algorithm:
- Merge sort:
- Bubble sort:
- Quick sort: (when the pivot is always the smallest or largest element)
- Selection sort:
grows slower than , so merge sort has the lowest worst case. Merge sort takes on every input because it always splits in halves.
Answer: (A)
- Merge sort:
- Bubble sort:
- Quick sort: (when the pivot is always the smallest or largest element)
- Selection sort:
grows slower than , so merge sort has the lowest worst case. Merge sort takes on every input because it always splits in halves.
Answer: (A)
Exponentiation is a heavily used operation in public key cryptography. Which of the following options is the tightest upper bound on the number of multiplications required to compute ?
Answer: (A)
Use repeated squaring. For :
if is even, and if is odd.
Take the remainder mod after every multiplication, so all numbers stay below . Each step needs one squaring, plus one more multiplication when is odd, and then the exponent is cut to . So the number of multiplications satisfies , with .
The exponent halves at most times, so .
This cannot be improved: one multiplication at most doubles the exponent already reached, so reaching needs at least multiplications. So is the tightest bound among the options.
Answer: (A)
if is even, and if is odd.
Take the remainder mod after every multiplication, so all numbers stay below . Each step needs one squaring, plus one more multiplication when is odd, and then the exponent is cut to . So the number of multiplications satisfies , with .
The exponent halves at most times, so .
This cannot be improved: one multiplication at most doubles the exponent already reached, so reaching needs at least multiplications. So is the tightest bound among the options.
Answer: (A)
Consider the following C code segment:
int IsPrime(n)
{
int i,n;
for(i=2;i<=sqrt(n);i++)
if(n%i == 0)
{printf("Not Primen"); return 0;}
return 1;
}
Let T(n)denote the number of times the for loop is executed by the program on
input n. Which of the following is TRUE?Answer: (C)
The loop variable runs over , and the function returns early when divides .
Upper bound: the loop never runs more than about times, so . For a prime no divisor is found and it runs about times.
Lower bound: for every the loop runs at least once, so . If is even, then at the first pass () and the function returns, so for all such .
Because of these even inputs, does not hold, which rules out (A) and (B). In (C), both parts are true.
Answer: (C)
Upper bound: the loop never runs more than about times, so . For a prime no divisor is found and it runs about times.
Lower bound: for every the loop runs at least once, so . If is even, then at the first pass () and the function returns, so for all such .
Because of these even inputs, does not hold, which rules out (A) and (B). In (C), both parts are true.
Answer: (C)
What is the time complexity of the following recursive function:
int DoSomething (int n)
{
if (n <= 2)
return 1;
else
return (DoSomething (floor(sqrt(n))) + n);
}Answer: (D)
The time is the number of recursive calls, since each call does constant work plus one recursive call: .
Expand: .
The calls stop when the argument reaches or less, so we need . Take of both sides: , so and . The floor in and the last call change this only by a constant, so .
Check with a number: for the arguments are . That is square roots and calls in all, and .
Answer: (D)
Expand: .
The calls stop when the argument reaches or less, so we need . Take of both sides: , so and . The floor in and the last call change this only by a constant, so .
Check with a number: for the arguments are . That is square roots and calls in all, and .
Answer: (D)
In the following C function, let nm.
int gcd(n,m)
{
if (n%m ==0) return m;
n = n%m;
return gcd(m,n);
} How many recursive calls are made by this function?Answer: (A)
Each call makes the next call . So the first arguments of the calls are . Two calls later, the first argument is .
Claim: .
If , then .
If , then .
So the first argument at least halves every two calls, and the number of calls is at most about , which is .
This is also reached: for consecutive Fibonacci numbers , each step only goes one Fibonacci number down, so it takes about calls. So the worst case is .
Answer: (A)
Claim: .
If , then .
If , then .
So the first argument at least halves every two calls, and the number of calls is at most about , which is .
This is also reached: for consecutive Fibonacci numbers , each step only goes one Fibonacci number down, so it takes about calls. So the worst case is .
Answer: (A)