1. Which one the following inplace sorting
algorithms needs the minimum number of swaps?
(A) Quick-sort (B) Insertion sort
(C) Selection sort (D) Heap sort
2. The median of n elements can be found in
O(n) time. Which one of the following is correct about the complexity of quick sort, in which median is selected as pivot? (A) (n)
(B) (n log n)
(C) (n2)
(D) (n3)
CS-2007
3. Which of the following sorting algorithms
has the lowest worst case complexity? (A) Merge sort
(B) Bubble sort
(C) Quick sort (D) Selection sort
CS-2008
4. Consider the Quicksort algorithm.
Suppose there is a procedure for finding a pivot element which splits the list into two sub lists each of which contains at least one-fifth of the elements. Let T(n) be the number of comparisons required to sort n elements. Then
(A) T(n) 2T (n/5) + n
(B) T(n) T(n/5) + T(4n/5) + n (C) T(n) 2T (4n/5) + n
(D) T(n) 2T (n/2) + n
CS-2009
5. What is the number of swaps required to
sort n elements using selection sort, in the worst case?
(A) (n) (B) (n log n)
(C) ( )
(D) ( g )
6. In quick sort, for sorting n elements, the
( ) the smallest element is selected as
pivot using an O(n) time algorithm. What
is the worst case time complexity of the quick sort? (A) (n) (B) (n log n) (C) ( ) (D) ( g ) CS-2011
7. An algorithm to find the length of the
longest monotonically increasing
sequence of number in an array A[0 :n 1] is given below.
Let denote the length of the longest
monotonically increasing sequence starting at index i in the array.
Initialize
F r a i such that 0 i 2
{ if [i] [i ]
ther ise Finally the length of the longest
monotonically increasing sequence is
Max( )
Which of the following statements is TRUE ?
(A) The algorithm uses dynamic programming paradigm (B) The algorithm has a linear
complexity and uses branch and bound paradigm
(C) The algorithm has a non-linear polynomial complexity and uses branch and bound paradigm (D) The algorithm uses divide and
conquer paradigm.
CS-2012
8. A list of nstrings, each of length n, is
sorted into lexicographic order using the merge-sort algorithm. The worst case running time of this computation is (A) O(n log n)
(B) O(n2 log n)
(C) O (n2 + log n)
CS-2013
9. Which one of the following is the tightest
upper bound that represents the number of swaps required to sort n numbers using selection sort?
(A) ( g ) (B) ( )
(C) ( g )
(D) ( )
10. The number of elements that can be
s rted i Θ( g ) time usi g heap s rt is (A) Θ( ) (B) Θ(√ g ) (C) Θ ( ) (D) Θ( g ) CS-2014
11. Let P be a quick sort program to sort
numbers in ascending order using the
first element as the pivot. Let t and t be
the number of comparisons made by P for the inputs [1 2 3 4 5 ] and [4 1 5 3 2] respectively. Which one of the following holds?
(A) t
(B) t t
(C) t t
(D) t t
12. Suppose P, Q, R, S, T are sorted sequences
having lengths 20,24,30,35,50
respectively. They are to be merged into a single sequence by merging together two sequences at a time. The number of comparisons that will be needed in the worst case by the optimal algorithm for doing this is ____.
13. You have an array of n elements. Suppose
you implement quicksort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the worst case performance is
(A) O( )
(B) O(n log n)
(C) Θ( g )
(D) O( )
Answer Keys and Explanations
1. [Ans. C]
In selection sort, minimum number of swaps taken compared to other sorting techniques.
No. of swaps = O(n)
No. of comparisons = O( )
In every pass, it constructs sorted sub- arrays and places the next element in proper position in the sorted sub array. So, we need only 1 swap in every pass
2. [Ans. C]
If the median of n elements can be found in O(n) time and we select the median as pivot. Then quicksort of n elements of an array [ ] …… [ ] as f s We permute the elements in the array so that for some index j with value v all the
records with key less than v appear in [ ] … [j 1] and all those with keys v r greater appear i [j ] … [ ] we then apply quicksort recursively to [ ] … A[j 1] a d [j ] … [ ] The recursion equation becomes for some value of i
( ) ∑( (i ) ( ))
he s uti f the ( ) ( g ) But to calculate median every time [pass]
it require at least (n2) time. Total time
3. [Ans. A] Sorting Algorithm
Worst case complexity (with n input)
Merge sort O(nlogn)
Bubble sort ( )
Quick sort ( )
Selection sort ( )
4. [Ans. B]
If we want to sort n elements with the help of Quicksort algorithm. If pivot elements which split the list into two sub- lists each in which one list contains one fifth element or n/5 and other list contains 4n/5 and balancing takes n. So ( ) ( / ) (4 / )
[Note : n ]
5. [Ans. A]
In each pass there can be at most 1 swap. There are n such passes in selection sort i.e. maximum n swaps will happen in worst case.
6. [Ans. B]
The relation T(n) = T(n/4)+T(3n/4)+n The pivot element is selected in a such
way that it will divide the array into 1/4th
and 3/4th always solving this relation
given (n log n).
7. [Ans. A]
The algorithm uses dynamic program paradigm.
8. [Ans. B]
In merge – sort algorithm no. of splits are proportional to height and in each level
work done is .
∴ ta ime C mp exity ( log n)
9. [Ans. B]
Selection sort
Selects the next big element and swaps with initial elements
Comparisons – O ( )
Swaps – O(n)
10. [Ans. C]
If we have an array of x elements then time complexity to sort x elements using heap sort is (x g x).
We need to find value of x so that time complexity to sort x elements gets ( g ) We can direct discard option (A) & (D). Now check option (C)
[ g g g g ( g g g )] ( g [ g ( ) g g ] ⏟ ) ( g ) ∴ pti (C) is c rrect 11. [Ans. C]
First input [1 2 3 4 5 ] is the worst case of quicksort and it will require more no.of comparisons than input 2 [4 1 5 3 2]
12. [Ans. 358]
When we are merging two sorted lists of lengths m and n, the maximum number of comparisons will be m+n 1. Also the worst case merging is done by merging the two smallest length sequences. The given lengths of sequences are: L1:20, L2:24, L3:30, L4:35, L5:50.
Step1: The number comparisons in merging sequences L1, L2 is
( 0 4 ) 4 (Sequence L6) Step2: The number comparisons in merging sequences L3, L4 is
Step 3: The number comparisons in merging sequences L6, L5 is
(44+50 1)=93 (sequence L8)
Step4: The number comparisons in merging sequences L8, L7 is
(94+65 1)=158 (final sequence)
So the total number of comparisons: 43+64+93+158 = 358
13. [Ans. A]
The worst case time complexity of quick
sort is ( ) irrespective of whether pivot
element is first element or middle element or last element. If the input array is such that middle element is the minimum or maximum one, then the worst case can occur