1. Home
  2. Computer Science MCQs
  3. C Programming
  4. Two records compare equal under a qsort comparator. What…

Added 30 Nov -0001

Two records compare equal under a qsort comparator. What does the C standard guarantee about their relative order after sorting?

  1. They keep their original relative order, since qsort is a stable sort
  2. Their order is reversed, because qsort always swaps equal elements
  3. Their order stays unspecified unless the comparator breaks the tie itself
  4. Nothing is guaranteed, because qsort is not required to be stable at all Correct answer

Correct answer

D. Nothing is guaranteed, because qsort is not required to be stable at all

24 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs