ProgrammingHardGATE CS · Campus placements
qsort is called on int arr[5] = {-7, 3, -2, 5, -4} with a comparator that returns (abs(a) > abs(b)) - (abs(a) < abs(b)) on the two elements. What is the resulting order?
- A. -2 3 -4 5 -7
- B. -7 -4 -2 3 5
- C. 3 -2 -4 5 -7
- D. -2 -4 3 5 -7
Correct answer: A. -2 3 -4 5 -7
The comparator orders by absolute value only. The absolute values 2, 3, 4, 5, 7 are all distinct, so the ascending result is -2, 3, -4, 5, -7.
More C Programming questions
- A qsort comparator is written as return (const int )a - (const int )b; Which statement is true?
- Which statement about the standard library function qsort is true?
- 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)?