Two records compare equal under a qsort comparator. What does the C standard guarantee about their relative order after sorting?
Correct answer
D. Nothing is guaranteed, because qsort is not required to be stable at all
24 views
D. Nothing is guaranteed, because qsort is not required to be stable at all
24 views
qsort is specified only to order elements according to the comparator, not to preserve the original order of elements that compare equal, so any stable-looking result is an implementation detail. If a stable order matters, add a secondary key such as a sequence number inside the comparator rather than relying on the algorithm. Note that an unstable tie order is unspecified behaviour, quite different from undefined behaviour.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.