qsort descending order

S

subramanian100in

In the ISO document

www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

in the detail of the qsort library function in 7.20.5.2, the
description says that, the contents of the array are sorted into
ascending order.

MY QUESTION:
Does it mean to include descending order also ?

If not, suppose we write the comparison function in such a way that it
returns less than, equal to, or greater than zero if the first
argument is greater than, equal to, or less than the second argument,
we should be able to use the qsort to sort the array elements into
descending order.

Am I correct or wrong ? Kindly explain.
 
R

Richard Heathfield

(e-mail address removed), India said:
In the ISO document

www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

in the detail of the qsort library function in 7.20.5.2, the
description says that, the contents of the array are sorted into
ascending order.

MY QUESTION:
Does it mean to include descending order also ?

No. You define what "ascending" means in your comparison function, by
the way in which you associate the return value of the function with
the result of the comparison.
If not, suppose we write the comparison function in such a way that it
returns less than, equal to, or greater than zero if the first
argument is greater than, equal to, or less than the second argument,
we should be able to use the qsort to sort the array elements into
descending order.

Right.
 
O

osmium

In the ISO document

www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

in the detail of the qsort library function in 7.20.5.2, the
description says that, the contents of the array are sorted into
ascending order.

MY QUESTION:
Does it mean to include descending order also ?

If not, suppose we write the comparison function in such a way that it
returns less than, equal to, or greater than zero if the first
argument is greater than, equal to, or less than the second argument,
we should be able to use the qsort to sort the array elements into
descending order.

Am I correct or wrong ? Kindly explain.

I think the word "ascending" should not have been used in the specification.
 
R

Richard Bos

osmium said:
I think the word "ascending" should not have been used in the specification.

Why not? qsort() sorts the array in ascending order, with respect to the
comparison function supplied. This can be ascending /qua/ numerical
size, ascending /qua/ age struct member, or ascending /qua/ _inverse_ of
the number of spots.

Richard

Richard
 
C

CBFalconer

In the ISO document

www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

in the detail of the qsort library function in 7.20.5.2, the
description says that, the contents of the array are sorted into
ascending order.

MY QUESTION:
Does it mean to include descending order also ?

If not, suppose we write the comparison function in such a way that it
returns less than, equal to, or greater than zero if the first
argument is greater than, equal to, or less than the second argument,
we should be able to use the qsort to sort the array elements into
descending order.

Am I correct or wrong ? Kindly explain.

The only thing that defines order is the comparison function. So
you are correct.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
R

Richard Tobin

I think the word "ascending" should not have been used in the
specification.
[/QUOTE]

Because, apparently, it is confusing. Of course you can say that
3,2,1 are in ascending order with respect to a comparison function
that says 1>2>3, but is it the clearest way to do it? The only problem
is finding a better terminology - perhaps the terms "earlier" and
"later" could have been used?

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top