How would you use qsort to sort on a string

J

Jordan Abel

The question wasn't "how do I sort the letters in a string", but rather
"how do I do it without conditional statements". (See the original post,
not the thread subject.)

I think the answer that is wanted is to subtract the two characters for
the result of the comparison function. I.e. it's a question to see if
you "see" that solution
 
J

Jordan Abel

Not thinking very clearly this morning. It is unnecessary and
should be omitted.


It is not a "conditional statement" as far as I know. However, I
don't what a conditional statement is for sure; I assumed it
meant an "if" statement.

I'd have used "return *b - *a", and i suspect that's the answer the
question is fishing for with the "no conditionals" thing.
 
P

pete

Jordan said:
I'd have used "return *b - *a"

ITYM return *a - *b;

The type of (*a - *b) isn't guaranteed to be int,
it might be unsigned, in which case it won't work right.
 
B

Ben Pfaff

[for qsort() comparison function]
I'd have used "return *b - *a", and i suspect that's the answer the
question is fishing for with the "no conditionals" thing.

I wouldn't ever suggest using that form, because I've seen too
many people get bit by overflow using it. I've had multiple
people report "bugs" against GNU libavl, for example, when in
fact they used this trick in their comparison functions and that
caught up with them.
 
B

Bill Gutz

Ben said:
[for qsort() comparison function]

I'd have used "return *b - *a", and i suspect that's the answer the
question is fishing for with the "no conditionals" thing.


I wouldn't ever suggest using that form, because I've seen too
many people get bit by overflow using it. I've had multiple
people report "bugs" against GNU libavl, for example, when in
fact they used this trick in their comparison functions and that
caught up with them.

Right.

Search groups.google.com for "Kirby qsort". Here's a hit worth bookmarking:

http://tinyurl.com/duqc6
 
C

CBFalconer

Bill said:
.... snip ...

Search groups.google.com for "Kirby qsort". Here's a hit worth bookmarking:

http://tinyurl.com/duqc6

No so called tinyurl is worth bookmarking. They become obsolete,
and even worse, they give no indication of their eventual
destination. Supply a real URL instead.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
N

nicholas.wakefield

The question wasn't "how do I sort the letters in a string", but rather
I'm not sure what you define as conditional but for loops are
considered a conditional loop to me
for (p = s; *p != '\0'; p++)
for (i = 0; i <= UCHAR_MAX; i++) {
for (j = 0; j < counts; j++)


You don't have to use qsort to solve the problem, it was one of many
ways we though of.
 
J

Jordan Abel

No so called tinyurl is worth bookmarking. They become obsolete,
and even worse, they give no indication of their eventual
destination. Supply a real URL instead.

What i've heard is "good practice" is to include both a shortened
version and the real one, in case someone's newsreader can't handle the
real one due to it being too wide
 
K

Keith Thompson

CBFalconer said:
No so called tinyurl is worth bookmarking. They become obsolete,
and even worse, they give no indication of their eventual
destination. Supply a real URL instead.

<OT>
I wasn't aware that they can become obsolete (the tinyurl.com home
page doesn't say anything about that); I've always assumed they last
indefinitely. Your other pointers are valid, though. Also,
tinyurl.com advertises the use of their service to hide affiliate
URLs, which doesn't seem entirely ethical.
</OT>
 
C

CBFalconer

Bill said:

No apology needed. My purpose was to advise about the situation.
Nobody is expected to know everything or even to draw all the
possible conclusions. The combination (short/long) provides the
best of both worlds.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
J

Jordan Abel

No apology needed. My purpose was to advise about the situation.
Nobody is expected to know everything or even to draw all the
possible conclusions. The combination (short/long) provides the
best of both worlds.

When linking a google groups message, it might make sense to figure out
the old ?selm=Message-ID representation [which still works] - mainly
because it provides a reference point that does not depend on google
groups still existing in the future.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top