GATE Computer ScienceVery hardGATE CS (PYQ pattern)

A 4-input comparator network applies, in order, the comparators (0,1), (2,3), (0,2), (1,3), (1,2), each putting the smaller value on the lower-indexed wire. It sorts all inputs. If the last comparator (1,2) is removed, how many of the 24 permutations of 4 distinct keys does the shortened network still sort?

  1. A. 12
  2. B. 8
  3. C. 16
  4. D. 6

Correct answer: B. 8

After the first four comparators the minimum is on wire 0 and the maximum on wire 3; only wires 1 and 2 may still be out of order. Simulating all 24 permutations, the middle pair is already in order for 8 of them. (By the 0-1 principle, 4 of the 16 binary inputs fail without (1,2).)

More Algorithms questions

Exams that ask this topic