If one uses straight two-way merge sort algorithm to sort the following elements in ascending order:
then the order of these elements after second pass of the algorithm is:
GATE CSE 1999Algorithms › Sorting AlgorithmsVerified
(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
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.