ProgrammingHardTCS NQT · Infosys
Which statement about the standard library function qsort is true?
- A. It always keeps elements that compare equal in their original order
- B. It returns a newly allocated sorted array and leaves the input unchanged
- C. It is guaranteed to be stable whenever the element size is 4 bytes
- D. The C standard does not guarantee that elements comparing equal keep their original relative order
Correct answer: D. The C standard does not guarantee that elements comparing equal keep their original relative order
qsort sorts the array in place, and the standard does not require stability. Any implementation may reorder elements that compare equal, so a stable sort needs your own tie-breaker.
More C Programming questions
- char names[] holds char pointers to strings and must be sorted alphabetically with qsort(names, n, sizeof names[0], cmp). Which comparator…
- int f(int n) { return n < 2 ? 1 : f(n - 1) + f(n - 2); } How many calls to f happen in total, counting the first call, when evaluating f(5)?
- A recursive Tower of Hanoi solution moves n disks, one at a time, with the rule that a larger disk never sits on a smaller one. How many…
- The Ackermann function is defined as A(0,n) = n+1, A(m,0) = A(m-1,1), and A(m,n) = A(m-1, A(m,n-1)) for m, n > 0. What is A(2,3)?
- int a[5] = {10, 20, 30, 40, 50}; int p = a + 1, q = a + 4; printf("%td %d", q - p, (p + 2)); prints what?
- void f(int a[10]) { printf("%zu", sizeof a); } Which statement about the result is correct?