ProgrammingVery hard
A struct array of n records is sorted by key. The code stores a record index inside each record before sorting, so that it can recover the original order. Which problem does this solve?
- A. It makes qsort run in O(n) time
- B. It removes the need for a comparator
- C. It makes bsearch work on unsorted data
- D. It makes the sort effectively stable, because equal keys are ordered by their original index
Correct answer: D. It makes the sort effectively stable, because equal keys are ordered by their original index
Adding the original index as a tiebreaker gives a total order, so the result is the same as a stable sort on the key.
More C Programming questions
- A histogram of sorted keys [1, 1, 2, 2, 2, 3] counts the adjacent equal pairs. How many such pairs are there?
- The largest gap between consecutive values of the sorted list 1, 4, 9, 16 is which value?
- The median of the sorted keys 2, 4, 6, 8 with integer division is which value?
- In the keys 3, 1, 3, 2, 1, the mode (most frequent key) is needed. With ties, the smallest key wins. Which is the mode?
- Records with a name and a score are stored in an array and sorted with a comparator that compares the names with strcmp. Which field…
- Sorting 2 x 10^6 structs with qsort in C takes about how many comparisons?