ProgrammingVery hard
qsort is used on an array of records where several records have equal keys. Which statement is guaranteed by the C standard?
- A. Equal records always keep their input order
- B. Equal records always come out reversed
- C. Records with equal keys may end up in a different relative order than they started in
- D. qsort is stable only when the array has under 10 records
Correct answer: C. Records with equal keys may end up in a different relative order than they started in
The standard does not require qsort to be stable. An implementation may reorder records with equal keys, so code that depends on their order must add a tiebreaker key.
More C Programming questions
- A sorted array of struct items is searched with bsearch for a key that is not in the array. What does bsearch return?
- An array struct P ps[] of pointers to records is to be sorted by an int field k with qsort. Which comparator is correct?
- For struct R { char c; short s; int i; } with natural alignment (char 1, short 2, int 4), what is sizeof(struct R)?
- Assume natural alignment with 4-byte int. What is sizeof(struct R[5]) where struct R is the eight-byte type from the previous record?
- struct Q { char name[6]; double score; char tag; }; with 8-byte double and natural alignment. What is sizeof(struct Q)?
- Assume natural alignment: char 1, double 8. What is sizeof(struct T) for struct T { char a; double d; char b[3]; }?