Skip to main content

GATE CSE 1999Algorithms › Sorting AlgorithmsVerified

GATE CSE 1999 PYQ: If one uses straight two-way merge sort algorithm to sort the following elements in ascending order: 20 , 47…

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, 1720, \ 47, \ 15, \ 8, \ 9, \ 4, \ 40, \ 30, \ 12, \ 17

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

  1. A

    8, 9, 15, 20, 47, 4, 12, 17, 30, 408, \ 9, \ 15, \ 20, \ 47, \ 4, \ 12, \ 17, \ 30, \ 40

  2. B

    8, 15, 20, 47, 4, 9, 30, 40, 12, 178, \ 15, \ 20, \ 47, \ 4, \ 9, \ 30, \ 40, \ 12, \ 17

    Correct answer
  3. C

    15, 20, 47, 4, 8, 9, 12, 30, 40, 1715, \ 20, \ 47, \ 4, \ 8, \ 9, \ 12, \ 30, \ 40, \ 17

  4. D

    4, 8, 9, 15, 20, 47, 12, 17, 30, 404, \ 8, \ 9, \ 15, \ 20, \ 47, \ 12, \ 17, \ 30, \ 40

Answer

(B) 8 , 15 , 20 , 47 , 4 , 9 , 30 , 40 , 12 , 17 8, \ 15, \ 20, \ 47, \ 4, \ 9, \ 30, \ 40, \ 12, \ 17 8 , 15 , 20 , 47 , 4 , 9 , 30 , 40 , 12 , 17

Approach

Treat straight two-way merge sort as iterative bottom-up merge sort: initially each key is a singleton sorted run. Each pass merges adjacent runs pairwise, doubling the sorted run length. After the first pass, runs have length at most 2; after the second pass, runs have length at most 4.

The second pass merges sorted pairs into sorted groups of up to four elements, producing $[8,15,20,47]$, $[4,9,30,40]$, and $[12,17]$. Therefore the correct order is B.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.