Data Structures & AlgorithmsHard
When sorting records by key with counting sort, why must the placement pass proceed in a specific direction for stability?
- A. Stability is irrelevant to counting sort.
- B. The direction only affects the speed, not the order.
- C. Placing elements from the end of the input backwards into their slots keeps equal keys in their original order.
- D. The keys must be placed from largest to smallest.
Correct answer: C. Placing elements from the end of the input backwards into their slots keeps equal keys in their original order.
Equal keys stay in input order when the output positions are filled from the last input element backwards, so stable placement is a deliberate direction choice.
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 cycle-based 'place each value at its index' method find missing numbers in linear time?
- Why does Floyd's method for finding a duplicate in an array of n+1 values in 1..n view the array as a linked structure?
- Stable sorting by two keys works by sorting twice. Which order of passes gives the lexicographic order with the first key as primary?
- 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?