GATE / CS ARCHIVE

GATE CSE 1991 / QUESTION 1(vii)

Optimal comparisons for five keys

Algorithms · Searching, Sorting & Selection · Full GATE CSE 1991 paper

GATE 1991, Q1(vii) 2 marks · Other
Report issue

Searching, Sorting & Selection

Question 1(vii), GATE 1991: What minimum number of comparisons suffices to sort five arbitrary distinct keys?
Question 1(vii), GATE 1991: What minimum number of comparisons suffices to sort five arbitrary distinct keys?
Tap to enlarge
Answer & solution

Answer: 7 in the optimal worst case (no official key was published)

There are 5!=120 possible strict orders, and a binary comparison tree of height six distinguishes at most 64 outcomes. Thus at least seven comparisons are necessary in the worst case. For sufficiency, sort two pairs (two comparisons), compare their larger elements (one), and rename them so a≤b≤d with c≤d. Insert the fifth key into a, b, d in at most two comparisons. Insert c into the prefix preceding d: at most three elements precede d, giving at most four possible positions and two further comparisons. Total: seven. This is a worst-case guarantee; a best-case successful path can be shorter.

Original paper, page 1