GATE 2003
107 questions
MCQ 1071-mark 107
Questions 66–70 of 107
The usual implementation of Insertion Sort to sort an array uses linear search to identify the position where an element is to be inserted into the already sorted part of the array. If, instead, we use binary search to identify the position, the worst case running time will
Answer: (A)
Insert the -th element into the sorted part, which has elements.
Binary search finds the position with about comparisons, instead of up to for linear search.
But in an array, making room means shifting the larger elements one place to the right. In the worst case (reverse sorted input) all elements move, whatever way the position was found.
Worst case total: .
The shifting dominates, so the worst case stays .
Answer: (A)
Binary search finds the position with about comparisons, instead of up to for linear search.
But in an array, making room means shifting the larger elements one place to the right. In the worst case (reverse sorted input) all elements move, whatever way the position was found.
Worst case total: .
The shifting dominates, so the worst case stays .
Answer: (A)
The cube root of a natural number n is defined as the largest natural number m such that . The complexity of computing the cube root of n(n is represented in binary notation) is
Answer: (C)
Here is written in binary, so it has about bits.
Upper bound: binary search over for the largest with . At each step take the middle value , compute and compare it with . If move up, else move down. The range halves each time, so there are about steps. Each step multiplies numbers of bits, and ordinary multiplication takes bit operations. So the total is , a power of .
(A) is wrong because is far smaller than , so the cost is . (B) is wrong because the cost is with .
Lower bound: the cube root has about bits, and the algorithm must write all of them. So the time is . Since grows faster than any power of , the cost is not for any constant . This rules out (D).
Answer: (C)
Upper bound: binary search over for the largest with . At each step take the middle value , compute and compare it with . If move up, else move down. The range halves each time, so there are about steps. Each step multiplies numbers of bits, and ordinary multiplication takes bit operations. So the total is , a power of .
(A) is wrong because is far smaller than , so the cost is . (B) is wrong because the cost is with .
Lower bound: the cube root has about bits, and the algorithm must write all of them. So the time is . Since grows faster than any power of , the cost is not for any constant . This rules out (D).
Answer: (C)
Consider the following three claims
1. , where k and m are constants
2.
3.
Which of these claims are correct?
1. , where k and m are constants
2.
3.
Which of these claims are correct?
Answer: (A)
Claim 1. For constants and and all : , so . The bounds hold with constants 1 and , so . (For negative , use , where .) True.
Claim 2. , so it is with constant 2. True.
Claim 3. , so . This ratio grows without bound, so no constant gives . False.
Claims 1 and 2 are correct.
Answer: (A)
Claim 2. , so it is with constant 2. True.
Claim 3. , so . This ratio grows without bound, so no constant gives . False.
Claims 1 and 2 are correct.
Answer: (A)
Consider the following recurrence relation
T(1)=1
T(n+1)=T(n)+ for all n 1
The value of T( ) for m 1 is
T(1)=1
T(n+1)=T(n)+ for all n 1
The value of T( ) for m 1 is
Answer: (B)
Since , unrolling the recurrence gives .
The value of is for , which is numbers. For example, give 1 (three numbers), and give 2 (five numbers). The last number gives once. So
.
Using and :
.
Check with : , and (B) gives . (A) gives 59 and (D) gives 297.5.
Answer: (B)
The value of is for , which is numbers. For example, give 1 (three numbers), and give 2 (five numbers). The last number gives once. So
.
Using and :
.
Check with : , and (B) gives . (A) gives 59 and (D) gives 297.5.
Answer: (B)
Consider the following graph
Among the following sequences
(I) a b e g h f
(II) a b f e h g
(III) a b f h g e
(IV) a f g h b e
Which are depth first traversals of the above graph?
Among the following sequences
(I) a b e g h f
(II) a b f e h g
(III) a b f h g e
(IV) a f g h b e
Which are depth first traversals of the above graph?
Answer: (D)
Edges of the graph: .
DFS moves from the current vertex to an unvisited neighbour. It goes back (backtracks) only when the current vertex has no unvisited neighbour left.
I : , , , , are all edges to new vertices. Valid.
II : and are not joined by an edge. Also still has unvisited neighbours and , so DFS cannot leave by backtracking. Not valid.
III : , , , , are all edges to new vertices. Valid.
IV : , , , , are all edges to new vertices. Valid.
So I, III and IV are DFS traversals. Options (A) and (C) include II, and (B) leaves out III.
Answer: (D)
DFS moves from the current vertex to an unvisited neighbour. It goes back (backtracks) only when the current vertex has no unvisited neighbour left.
I : , , , , are all edges to new vertices. Valid.
II : and are not joined by an edge. Also still has unvisited neighbours and , so DFS cannot leave by backtracking. Not valid.
III : , , , , are all edges to new vertices. Valid.
IV : , , , , are all edges to new vertices. Valid.
So I, III and IV are DFS traversals. Options (A) and (C) include II, and (B) leaves out III.
Answer: (D)