Skip to content
Official PYQ · 1999

GATE CS 1999 Q1.14 — Searching, Sorting, Hashing

Explain with AI

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:

Choose one option
View worked solution

Answer: B

In a straight two-way merge sort (also known as iterative or bottom-up merge sort), we start by considering each element as a sorted run of size 11. In each subsequent pass, we merge adjacent sorted runs of size 2k−12^{k-1} to form sorted runs of size 2k2^k.

Let's trace the algorithm step-by-step for the given array:

Initial Array: [20],[47],[15],[8],[9],[4],[40],[30],[12],[17]\text{Initial Array: } [20], [47], [15], [8], [9], [4], [40], [30], [12], [17]

Pass 1:

Merge adjacent runs of size 11 to form sorted runs of size 22:

  • Merge [20][20] and [47]  ⟹  [20,47][47] \implies [20, 47]
  • Merge [15][15] and [8]  ⟹  [8,15][8] \implies [8, 15]
  • Merge [9][9] and [4]  ⟹  [4,9][4] \implies [4, 9]
  • Merge [40][40] and [30]  ⟹  [30,40][30] \implies [30, 40]
  • Merge [12][12] and [17]  ⟹  [12,17][17] \implies [12, 17]

Array after Pass 1:

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

Pass 2:

Merge adjacent runs of size 22 to form sorted runs of size 44:

  • Merge [20,47][20, 47] and [8,15]  ⟹  [8,15,20,47][8, 15] \implies [8, 15, 20, 47]
  • Merge [4,9][4, 9] and [30,40]  ⟹  [4,9,30,40][30, 40] \implies [4, 9, 30, 40]
  • The remaining run [12,17][12, 17] has no adjacent run to merge with, so it remains as is: [12,17][12, 17]

Array after Pass 2:

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

This matches the sequence given in Option B.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Searching, Sorting, Hashing questions