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?
- A. Sort by the primary key first, then stably by the secondary key.
- B. Sort by the secondary key first, then stably by the primary key.
- C. Sort by both keys in one pass with random tie-breaking.
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Why does the median of two sorted arrays of sizes m and n not require merging both arrays fully?
- Which property makes the number of inversions of a rotated sorted array equal to a simple product?
- When merging two sorted lists with the standard two-pointer method, how many comparisons are made in the worst case?
- When computing the k-th smallest element of two sorted arrays, why can a binary search on the answer's position replace a full merge?
- Why do the positions of a stable partition by parity keep their relative order, and why is that important?
- The sorted array [2, 3, 4, 7, 11] contains some positive integers. What is the 5th positive integer that does NOT appear in this array?