Data Structures & AlgorithmsHard

Stable sorting by two keys works by sorting twice. Which order of passes gives the lexicographic order with the first key as primary?

  1. A. Sort by the primary key first, then stably by the secondary key.
  2. B. Sort by the secondary key first, then stably by the primary key.
  3. C. Sort by both keys in one pass with random tie-breaking.
  4. D. Sort by the primary key in descending order only.

Correct answer: B. Sort by the secondary key first, then stably by the primary key.

The last stable pass dominates, so the primary key must be sorted last. Earlier passes order ties correctly within each primary group.

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

Exams that ask this topic