## Sorting

 Question 1

A two dimensional array A[1...n][1...n] of integers is partially sorted if

```    ∀i, j ∈ [1...n−1],   A[i][j] < A[i][j+1] and
A[i][j] < A[i+1][j] ```

Fill in the blanks:
(a) The smallest item in the array is at A[i][j] where i=............and j=..............
(b) The smallest item is deleted. Complete the following O(n) procedure to insert item x (which is guaranteed to be smaller than any item in the last row or column) still keeping A partially sorted.

```procedure  insert (x: integer);
var        i,j: integer;
begin
(1) i:=1; j:=1, A[i][j]:=x;
(2) while (x > ...... or x > ......) do
(3) if A[i+1][j] < A[i][j] ......... then begin
(4) A[i][j]:=A[i+1][j]; i:=i+1;
(5) end
(6) else begin
(7) ............
(8) end
(9) A[i][j]:= .............
end  ```
 A Theory Explanation.
Algorithms       Sorting       GATE 1996
 Question 2

A sorting technique is called stable if

 A it takes O (nlog n) time B it maintains the relative order of occurrence of non-distinct elements C it uses divide and conquer paradigm D it takes O(n) space
Algorithms        Sorting       GATE 1999
Question 2 Explanation:
Sorting techniques are said to be stable if it maintains the relative order of occurrence of non-distinct element.
 Question 3

If one uses straight two-way merge sort algorithm to sort the following elements in ascending order:

`   20, 47, 15, 8, 9, 4, 40, 30, 12, 17 `

then the order of these elements after second pass of the algorithm is:

 A 8, 9, 15, 20, 47, 4, 12, 17, 30, 40 B 8, 15, 20, 47, 4, 9, 30, 40, 12, 17 C 15, 20, 47, 4, 8, 9, 12, 30, 40, 17 D 4, 8, 9, 15, 20, 47, 12, 17, 30, 40
Algorithms        Sorting       GATE 1999
Question 3 Explanation: Question 4

Let s be a sorted array of n integers. Let t(n) denote the time taken for the most efficient algorithm to determined if there are two elements with sum less than 1000 in s. Which of the following statements is true?

 A t(n) is O(1) B n ≤ t(n) ≤ n log2 n C n log2 n ≤ t(n) < (n/2) D t(n) = (n/2)
Algorithms        Sorting       GATE 2000
Question 4 Explanation:
Since the array is sorted. Now just pick the first two minimum elements and check if their sum is less than 1000 or not. If it is less than 1000 then we found it and if not then it is not possible to get the two elements whose sum is less than 1000. Hence, it takes constant time. So, correct option is (A).
 Question 5

In a permutation a1...an of n distinct integers, an inversion is a pair (ai, aj) such that i < j and ai > aj.

If all permutations are equally likely, what is the expected number of inversions in a randomly chosen permutation of 1...n ?

 A n(n-1)/2 B n(n-1)/4 C n(n+1)/4 D 2n[log2n]
Algorithms        Sorting       GATE 2003
Question 5 Explanation:
Probability of inverse (ai, aj(i Probability of expected no. of inversions = (1/2) × (n(n-1)/2) = n(n-1)/4
 Question 6

In a permutation a1...an of n distinct integers, an inversion is a pair (ai, aj) such that ii > aj.

What would be the worst case time complexity of the Insertion Sort algorithm, if the inputs are restricted to permutations of 1...n with at most n inversions?

 A Θ(n2) B Θ(n log n) C Θ(n1.5) D Θ(n)
Algorithms        Sorting       GATE 2003
Question 6 Explanation:
Here the inputs are to be restricted to 1...n with atmost 'n' inversions. Then the worst case time complexity of inversion sort reduces to Θ(n).
 Question 7

Which one of the following in place sorting algorithms needs the minimum number of swaps?

 A Quick sort B Insertion sort C Selection sort D Heap sort
Algorithms        Sorting       GATE 2006
Question 7 Explanation:
Selection sort requires minimum number of swaps i.e O(n). The algorithm finds the minimum value, swaps it with the value in the first position, and repeats these steps for the remainder of the list. It does no more than n swaps, and thus is useful where swapping is very expensive.
 Question 8

Merge sort uses

 A Divide and conquer strategy B Backtracking approach C Heuristic search D Greedy approach
Algorithms       Sorting       GATE 1995
Question 8 Explanation:
Merge sort uses the divide and conquer strategy.
 Question 9

Consider the following sequence of numbers

`  92, 37, 52, 12, 11, 25  `

Use bubble sort to arrange the sequence in ascending order. Give the sequence at the end of each of the first five passes.

 A Theory Explanation.
Algorithms       Sorting       GATE 1995
 Question 10

Consider the recursive algorithm given below:

``` procedure bubblersort (n);
var i,j: index; temp : item;
begin
for i:=1 to n-1 do
if A[i] > A [i+1] then
begin
temp : A[i];
A[i]:=A[i+1];
A[i+1]:=temp
end;
bubblesort (n-1)
end ```

Let an be the number of times the ‘if…then….’ Statement gets executed when the algorithm is run with value n. Set up the recurrence relation by defining an in terms of an-1. Solve for an.

 A Theory Explanation.
Algorithms       Sorting       GATE 1993
 Question 11

Let a and b be two sorted arrays containing n integers each, in non-decreasing order. Let c be a sorted array containing 2n integers obtained by merging the two arrays a and b. Assuming the arrays are indexed starting from 0, consider the following four statements

```1. a[i] ≥ b [i] => c[2i] ≥ a [i]
2. a[i] ≥ b [i] => c[2i] ≥ b [i]
3. a[i] ≥ b [i] => c[2i] ≤ a [i]
4. a[i] ≥ b [i] => c[2i] ≤ b [i] ```
Which of the following is TRUE?

 A only I and II B only I and IV C only II and III D only III and IV
Programming       Sorting       GATE 2005-IT
Question 11 Explanation:
a[i] ≥ b[i]
Since both 'a' and 'b' are sorted in the beginning, there are 'i' elements than or equal to a[i] and similarly 'i' elements smaller than or equal to b[i]. So, a[i] ≥ b[i] means there are 2i elements smaller than or equal to a[i] and hence in the merged array, a[i] will come after these 2i elements. So, c[2i] ≤ a[i].
Similarly, a[i] ≥ b[i] says for b that, there are not more than 2i elements smaller than b[i] in the sorted array. So, b[i] ≤ c[2i].
So, option (C) is correct.
 Question 12

If we use Radix Sort to sort n integers in the range [nk/2, nk], for some k>0 which is independent of n, the time taken would be?

 A Θ(n) B Θ(kn) C Θ(nlogn) D Θ(n2)
Algorithms        Sorting       GATE 2008-IT
Question 12 Explanation:
Time complexity for radix sort = Θ(wn)
where n = keys, w = word size
w = log2 (nk) = k × log2 (n)
Complexity Θ(wn) = Θ(k × log2(n) × n) = Θ(n log n) = Θ(n log n)
 Question 13

Let P be a quicksort program to sort numbers in ascending order. Let t1 and t2 be the time taken by the program for the inputs [1 2 3 4] and [5 4 3 2 1] respectively. Which of the following holds?

 A t1 = t2 B t1 > t2 C t1 < t2 D t1 = t2 + 5 log 5
Algorithms        Sorting       GATE 1987       Video-Explanation
Question 13 Explanation:
Since both are in sorted order and no. of elements in second list is greater.
 Question 14

Quicksort is ________ efficient than heapsort in the worst case.

 A LESS.
Algorithms        Sorting       GATE 1988       Video-Explanation
Question 14 Explanation:
As worst case time for quicksort is O(n2) and worst case for heap sort is O(n logn).
 Question 15

The minimum number of comparisons required to sort 5 elements is _____

 A 7
Algorithms       Sorting       GATE 1991       Video-Explanation
Question 15 Explanation:
Minimum no. of comparisons
= ⌈log(n!)⌉
= ⌈log(5!)⌉
= ⌈log(120)⌉
= 7
 Question 16

Following algorithm(s) can be used to sort n integers in the range [1...n3] in O(n) time

 A Heapsort B Quicksort C Mergesort D Radixsort
Algorithms       Sorting       GATE 1992       Video-Explanation
Question 16 Explanation:
As no comparison based sort can ever do any better than nlogn. So option (A), (B), (C) are eliminated. O(nlogn) is lower bound for comparison based sorting.
As Radix sort is not comparison based sort (it is counting sort), so can be done in linear time.
 Question 17

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
Algorithms       Sorting       GATE 2008
Question 17 Explanation:
Consider the case where one subset of set of n elements contains n/5 elements and another subset of set contains 4n/5 elements.
So, T(n/5) comparisons are needed for the first subset and T(4n/5) comparisons needed for second subset.
Now, suppose that one subset contains more than n/5 elements then another subset will contain less than 4n/5 elements. Due to which time complexity will be less than
T(n/5) + T(4n/5) + n
Because recursion tree will be more balanced.
 Question 18
Assume that the algorithms considered here sort the input sequences in ascending order. If the input is already in ascending order, which of the following are TRUE?
I. Quicksort runs in Θ(n2) time
II. Bubblesort runs in Θ(n2) time
III. Mergesort runs in Θ(n) time
IV. Insertion sort runs in Θ(n) time
 A I and II only B I and III only C II and IV only D I and IV only
Algorithms       Sorting       GATE 2016 [Set-2]       Video-Explanation
Question 18 Explanation:
If input sequence is already sorted then the time complexity of Quick sort will take O(n2) and Bubble sort will take O(n) and Merge sort will takes O(nlogn) and insertion sort will takes O(n).
→ The recurrence relation for Quicksort, if elements are already sorted,
T(n) = T(n-1)+O(n) with the help of substitution method it will take O(n2).
→ The recurrence relation for Merge sort, is elements are already sorted,
T(n) = 2T(n/2) + O(n) with the help of substitution method it will take O(nlogn).
We can also use master's theorem [a=2, b=2, k=1, p=0] for above recurrence.
 Question 19

The worst case running times of Insertion sort, Merge sort and Quick sort, respectively, are:

 A Θ(nlogn), Θ(nlogn), and Θ(n2) B Θ(n2 ), Θ(n2 ), and Θ(nlogn) C Θ(n2), Θ(nlogn), and Θ(nlogn) D Θ(n2), Θ(nlogn), and Θ(n2)
Algorithms       Sorting       GATE 2016 [Set-1]       Video-Explanation
Question 19 Explanation: Question 20

Randomized quicksort is an extension of quicksort where the pivot is chosen randomly. What is the worst case complexity of sorting n numbers using randomized quicksort?

 A O(n) B O(n log n) C O(n2) D O(n!)
Algorithms        Sorting       GATE 2001
Question 20 Explanation:
In worst case Randomized quicksort execution time complexity is same as quicksort.
 Question 21

An array of 25 distinct elements is to be sorted using quicksort. Assume that the pivot element is chosen uniformly at random. The probability that the pivot element gets placed in the worst possible location in the first round of partitioning (rounded off to 2 decimal places) is _____.

 A 0.08 B 0.01 C 1 D 8
Algorithms       Sorting       GATE 2019       Video-Explanation
Question 21 Explanation:
Step-1: Given, 25 distinct elements are to be sorted using quicksort.
Step-2: Pivot element = uniformly random.
Step-3: Worst case position in the pivot element is either first (or) last.
Step-4: So total 2 possibilities among 25 distinct elements
= 2/25
= 0.08
 Question 22

There are n unsorted arrays: A1, A2, ..., An. Assume that n is odd. Each of A1, A2, ..., An contains n distinct elements. There are no common elements between any two arrays. The worst-case time complexity of computing the median of the medians of A1, A2, ..., An is

 A O(n) B O(n log n) C Ω(n2 log n) D O(n2)
Algorithms       Sorting       GATE 2019       Video-Explanation
Question 22 Explanation:
Finding the median in an unsorted array is O(n).
But it is similar to quicksort but in quicksort, partitioning will take extra time.
→ Find the median will be (i+j)/2
1. If n is odd, the value is Ceil((i+j)/2)
2. If n is even, the value is floor((i+j)/2)
-> Here, total number of arrays are
⇒ O(n)*O(n)
⇒ O(n2)
Note:
They are clearly saying that all are distinct elements.
There is no common elements between any two arrays.
 Question 23

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 θ(n2) D θ(n2 logn)
Algorithms        Sorting       GATE 2009
Question 23 Explanation:
Selection sort – There is no Worst case input for selection sort. Since it searches for the index of kth minimum element in kth iteration and then in one swap, it places that element into its correct position. For n-1 iterations of selection sort, it can have O(n) swaps. Selection Sort does a significant number of comparisons before moving each element directly to its final intended position. At most the algorithm requires N swaps. once we swap an element into place, you never go back again.So it is great for writes O(n) but not so great (at all) for reads — O(n2). It isn’t well-suited to generalized sorting, but might work well in specialized situations like EEPROM (where writes are inordinately expensive).
 Question 24

In quick sort, for sorting n elements, the (n/4)th 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 θ(n2) D θ(n2 log n)
Algorithms        Sorting       GATE 2009
Question 24 Explanation: n→n/(4/3)→n/(4/3)2→n/(4/3)3-----n/(4/3)k=1
n/(4/3)k = 1
⇒n=(4/3)k ⇒ k = log4/3n [k=no. of levels]
In each level workdone = Cn
So, total workdone = Cn⋅log4/3n = (nlog4/3n)
 Question 25

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 O(log n) B O(n) C O(n log n) D O(n2)
Algorithms       Sorting       GATE 2013       Video-Explanation
Question 25 Explanation:
Best, Average and worst case will take maximum O(n) swaps.
Selection sort time complexity O(n2) in terms of number of comparisons. Each of these scans requires one swap for n-1 elements (the final element is already in place).
 Question 26

The number of elements that can be sorted in Θ(log n) time using heap sort is

 A Θ(1) B Θ(√log⁡n) C Θ (log⁡n/log⁡log⁡n) D Θ(log n)
Algorithms       Sorting       GATE 2013       Video-Explanation
Question 26 Explanation:
Using heap sort to n elements it will take O(n log n) time. Assume there are log n/ log log n elements in the heap.
So, Θ((logn/ log log n)log(logn/log log n))
= Θ(logn/log logn (log logn - log log logn))
= Θ((log n/log logn) × log log n)
= Θ(log n)
Hence, option (C) is correct answer.
 Question 27

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(n2) B O(n log n) C Θ(n log⁡n) D O(n3)
Algorithms       Sorting       GATE 2014 [Set-3]       Video-Explanation
Question 27 Explanation:
The Worst case time complexity of quick sort is O (n2). This will happen when the elements of the input array are already in order (ascending or descending), irrespective of position of pivot element in array.
 Question 28

The worst case time complexity of Quick Sort is __________

 A O(n2) B O(log n) C O(n) D O(n logn)
Algorithms       Sorting       APPSC-2016-DL-CA
Question 28 Explanation:
The worst case time complexity of Quick Sort is O(n2).
The worst case quicksort happens only 2 times
1. Elements are already in sorted order.
2. All elements having the same weight.
 Question 29

The worst case time complexity of Merge Sort is _______

 A O(n2) B O(log n) C O(n) D O(n logn)
Algorithms       Sorting       APPSC-2016-DL-CA
Question 29 Explanation:
The worst case time complexity of Merge Sort is O(n logn).
The merge sort will not depend on the type of input. It will give the best, average and worst case time complexity is O(nlogn) only.
 Question 30

Which of the following sorting procedures is the slowest?

 A Quick sort B Heap sort C Shell sort D Bubble sort
Algorithms       Sorting       APPSC-2016-DL-CA
Question 30 Explanation:
Bubble sorting is the slowest because the average time complexity of all other sorting algorithms given in options are less than O(n2) but for bubble sort it is O(n2).
 Question 31

Which of the following sorting methods would be most suitable for sorting a list which is almost sorted?

 A Bubble Sort B Insertion Sort C Selection Sort D Quick Sort
Algorithms       Sorting       APPSC-2016-DL-CA
Question 31 Explanation:
If the elements of the list are almost sorted then insertion is the best to be chosen because it will take O(n) time.
 Question 32

The running time of insertion sort is

 A O(n2) B O(n) C O(log n) D O(n log n)
Algorithms       Sorting       APPSC-2016-DL-CA
Question 32 Explanation:
The worst case time complexity of insertion sort algorithm is O(n2).
 Question 33

A sort which compares adjacent elements in a list and switches where necessary is _________

 A Insertion sort B Heap sort C Quick sort D Bubble sort
Algorithms       Sorting       APPSC-2016-DL-CA
Question 33 Explanation:
The given description in question is about bubble sort in which we compare adjacent elements in the list and switches where necessary.
 Question 34

The correct order of the efficiency of the following sorting algorithms according to their overall running time comparison is

 A insertion>selection>bubble B insertion>bubble>selection C selection>bubble>insertion D bubble >selection>insertion
Algorithms       Sorting       APPSC-2016-DL-CA
Question 34 Explanation:
Option 2 is the most satisfying option because best case time complexity for insertion sort and bubble sort is O(N) but for selection sort it is O(N2). So selection sort is the least efficient.
 Question 35

A sort which iteratively passes through a list to exchange the first element with any element less than it and then repeats with a new first element is called

 A insertion sort B selection sort C heap sort D quick sort
Algorithms       Sorting       APPSC-2016-DL-CA
Question 35 Explanation:
The given characteristics in question are of selection sort.
There are 35 questions to complete.

Register Now