Data Structures & AlgorithmsHard

Records are (letter, number): (b, 2), (a, 1), (c, 2), (a, 2), (b, 1), (c, 1). Apply a stable sort by LETTER first, then a stable sort by NUMBER. Give the final list of pairs in order, written as letter-number, separated by spaces.

  1. A. a1 b1 c1 b2 c2 a2
  2. B. a1 a2 b1 b2 c1 c2
  3. C. a1 a2 b2 b1 c2 c1
  4. D. a1 b1 c1 a2 b2 c2

Correct answer: D. a1 b1 c1 a2 b2 c2

Two stable passes give a lexicographic order with the last key dominant: sort by letter first, then by number, and equal numbers keep the letter order. Using the keys in the other order, or a single sort on both keys by tuple, gives different results.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic