Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #13087 > unrolled thread
| Started by | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| First post | 2011-11-11 09:00 -0500 |
| Last post | 2011-11-12 22:42 -0500 |
| Articles | 14 — 10 participants |
Back to article view | Back to comp.lang.c
qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-11 09:00 -0500
Re: qsort Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-11 06:56 -0800
Re: qsort Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-11-11 16:39 +0000
Re: qsort 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-12 10:19 -0800
Re: qsort John Gordon <gordon@panix.com> - 2011-11-11 15:20 +0000
Re: qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-11 14:22 -0500
Re: qsort pete <pfiland@mindspring.com> - 2011-11-11 20:27 -0500
Re: qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-12 19:10 -0500
Re: qsort Keith Thompson <kst-u@mib.org> - 2011-11-12 16:40 -0800
Re: qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-13 01:39 -0500
Re: qsort Ian Collins <ian-news@hotmail.com> - 2011-11-13 19:51 +1300
Re: qsort Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-14 02:14 +0200
Re: qsort Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-14 00:43 -0800
Re: qsort pete <pfiland@mindspring.com> - 2011-11-12 22:42 -0500
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2011-11-11 09:00 -0500 |
| Subject | qsort |
| Message-ID | <4ebd2a87$0$19698$bbae4d71@news.suddenlink.net> |
Seeing that in that last paramter of qsort and int is returned I guess
you could pass NULL to that parameter. Would you want to do that. I have a
series of doubles...
double prices[]={24.45,24,21};
I want to sort them from highest to lowest. Still not knowing C's semantics
well enough there's probably a simpler way but what kind of function would I
want to write to pass to that last parameter of qsort?
Bill
[toc] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2011-11-11 06:56 -0800 |
| Message-ID | <c90a242d-b4ae-4d17-bdaf-4b14b02d4e76@y42g2000yqh.googlegroups.com> |
| In reply to | #13087 |
On Nov 11, 4:00 pm, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> Seeing that in that last paramter of qsort and int is returned I guess
> you could pass NULL to that parameter. Would you want to do that. I have a
> series of doubles...
>
> double prices[]={24.45,24,21};
>
> I want to sort them from highest to lowest. Still not knowing C's semantics
> well enough there's probably a simpler way but what kind of function would I
> want to write to pass to that last parameter of qsort?
>
>
int compdoubles(const void *e1, const void *e2)
{
const double *d1 = e1;
const double *d2 = e2;
/* now you write the logic to compare the doubles, returning 0 if
they are equal, and -1 or +1 is they are unequal. Use *d1, *d2,
because you have pointers*/
}
/* in the main function */
qsort(prices, 3, sizeof(double), compdoubles);
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2011-11-11 16:39 +0000 |
| Message-ID | <0.e94a5ba691aa7dd240d5.20111111163949GMT.874nyay5nu.fsf@bsb.me.uk> |
| In reply to | #13090 |
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Nov 11, 4:00 pm, "Bill Cunningham" <nos...@nspam.invalid> wrote:
<snip>
>> double prices[]={24.45,24,21};
<snip>
> /* in the main function */
>
> qsort(prices, 3, sizeof(double), compdoubles);
It's almost always better to write
qsort(prices, 3, sizeof *prices, compdoubles);
in these cases. When the type has been lost, you can't do this, of
course, but if you can, I can't see any reason not to.
The gain is small since you usually can't change the array type without
having to make other changes to the qsort call, but it still saves the
reader a millisecond or two of checking that the size, at least, is
right.
[In this case the 3 can also be replaced, but that it less common in
real code.]
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2011-11-12 10:19 -0800 |
| Message-ID | <2038935.61.1321121971518.JavaMail.geo-discussion-forums@prep8> |
| In reply to | #13097 |
On Saturday, November 12, 2011 12:39:49 AM UTC+8, Ben Bacarisse wrote:
> Malcolm McLean <malcolm...@btinternet.com> writes:
>
> > On Nov 11, 4:00 pm, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> <snip>
> >> double prices[]={24.45,24,21};
> <snip>
> > /* in the main function */
> >
> > qsort(prices, 3, sizeof(double), compdoubles);
>
> It's almost always better to write
>
> qsort(prices, 3, sizeof *prices, compdoubles);
>
> in these cases. When the type has been lost, you can't do this, of
> course, but if you can, I can't see any reason not to.
>
> The gain is small since you usually can't change the array type without
> having to make other changes to the qsort call, but it still saves the
> reader a millisecond or two of checking that the size, at least, is
> right.
>
> [In this case the 3 can also be replaced, but that it less common in
> real code.]
>
> --
> Ben.
OK, this is the famous function pointer that can be reloaded in C. The qsort accepts function pointers customized. Thus, it is easy to delegate a function.
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-11-11 15:20 +0000 |
| Message-ID | <j9jefb$9ag$1@reader1.panix.com> |
| In reply to | #13087 |
In <4ebd2a87$0$19698$bbae4d71@news.suddenlink.net> "Bill Cunningham" <nospam@nspam.invalid> writes:
> Seeing that in that last paramter of qsort and int is returned I guess
> you could pass NULL to that parameter. Would you want to do that.
No, you would not want to pass NULL. The purpose of that parameter is so
you can pass a pointer to a function that compares two of your data
elements. If you pass NULL, qsort has no way to compare the elements.
> ... what kind of function would I want to write to pass to that last
> parameter of qsort?
If you knew the parameter was for passing in a comparison function, why did
you ask about passing NULL?
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2011-11-11 14:22 -0500 |
| Message-ID | <4ebd7608$0$15508$bbae4d71@news.suddenlink.net> |
| In reply to | #13092 |
John Gordon wrote:
[snip]
> If you knew the parameter was for passing in a comparison function,
> why did
> you ask about passing NULL?
I really don't know anything about qsort. I know that NULL can be passed
in some cases to some function parameters. But as you say I guess this is
not one of those functions you'd ever want to do that with for *any* reason.
Bill
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2011-11-11 20:27 -0500 |
| Message-ID | <4EBDCB7A.2B15@mindspring.com> |
| In reply to | #13087 |
Bill Cunningham wrote:
> double prices[]={24.45,24,21};
>
> I want to sort them from highest to lowest.
> Still not knowing C's semantics
> well enough there's probably a simpler way
> but what kind of function would I
> want to write to pass to that last parameter of qsort?
/* BEGIN output from new.c */
From highest to lowest:
24.450000
24.000000
21.000000
/* END output from new.c */
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
int
price_comp(const void *a, const void *b)
{
const double *aa = a;
const double *bb = b;
return *aa > *bb ? -1 : *aa != *bb;
}
int
main(void)
{
double prices[] = {24.45, 24, 21};
double *ptr = prices;
size_t n = sizeof prices / sizeof *prices;
qsort(prices, n, sizeof *prices, price_comp);
puts("\n/* BEGIN output from new.c */\n");
puts("From highest to lowest:");
while (n-- != 0) {
printf("%f\n", *ptr);
++ptr;
}
puts("\n/* END output from new.c */");
return 0;
}
/* END new.c */
--
pete
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2011-11-12 19:10 -0500 |
| Message-ID | <4ebf0af7$0$19698$bbae4d71@news.suddenlink.net> |
| In reply to | #13159 |
pete wrote:
> return *aa > *bb ? -1 : *aa != *bb;
Can you show me that for loop that's not in shorthand? This is kind of
hard to read.
Bill
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-11-12 16:40 -0800 |
| Message-ID | <ln1utc6eip.fsf@nuthaus.mib.org> |
| In reply to | #13218 |
"Bill Cunningham" <nospam@nspam.invalid> writes:
> pete wrote:
>> return *aa > *bb ? -1 : *aa != *bb;
>
> Can you show me that for loop that's not in shorthand? This is kind of
> hard to read.
What for loop?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2011-11-13 01:39 -0500 |
| Message-ID | <4ebf6637$0$19698$bbae4d71@news.suddenlink.net> |
| In reply to | #13221 |
Keith Thompson wrote: > "Bill Cunningham" <nospam@nspam.invalid> writes: >> pete wrote: >>> return *aa > *bb ? -1 : *aa != *bb; >> >> Can you show me that for loop that's not in shorthand? This is >> kind of hard to read. > > What for loop? Ok I saw the ?: and thought it might be part of for. Bill
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-11-13 19:51 +1300 |
| Message-ID | <9i97n4FmrtU2@mid.individual.net> |
| In reply to | #13232 |
On 11/13/11 07:39 PM, Bill Cunningham wrote: > Keith Thompson wrote: >> "Bill Cunningham"<nospam@nspam.invalid> writes: >>> pete wrote: >>>> return *aa> *bb ? -1 : *aa != *bb; >>> >>> Can you show me that for loop that's not in shorthand? This is >>> kind of hard to read. >> >> What for loop? > > Ok I saw the ?: and thought it might be part of for. Doesn't a for loop normally include the word "for"? -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Phil Carmody <thefatphil_demunged@yahoo.co.uk> |
|---|---|
| Date | 2011-11-14 02:14 +0200 |
| Message-ID | <87sjlrblw1.fsf@bazspaz.fatphil.org> |
| In reply to | #13233 |
Ian Collins <ian-news@hotmail.com> writes: > On 11/13/11 07:39 PM, Bill Cunningham wrote: > > Keith Thompson wrote: > >> "Bill Cunningham"<nospam@nspam.invalid> writes: > >>> pete wrote: > >>>> return *aa> *bb ? -1 : *aa != *bb; > >>> > >>> Can you show me that for loop that's not in shorthand? This is > >>> kind of hard to read. > >> > >> What for loop? > > > > Ok I saw the ?: and thought it might be part of for. > > Doesn't a for loop normally include the word "for"? Ok I saw the From: line and thought it might be part of a troll. Phil -- Unix is simple. It just takes a genius to understand its simplicity -- Dennis Ritchie (1941-2011), Unix Co-Creator
[toc] | [prev] | [next] | [standalone]
| From | Nick Keighley <nick_keighley_nospam@hotmail.com> |
|---|---|
| Date | 2011-11-14 00:43 -0800 |
| Message-ID | <0f64cfaf-3d9d-4b85-8ca7-a4a2b1da0c24@e3g2000vbs.googlegroups.com> |
| In reply to | #13233 |
On Nov 13, 6:51 am, Ian Collins <ian-n...@hotmail.com> wrote: > On 11/13/11 07:39 PM, Bill Cunningham wrote: > > > Keith Thompson wrote: > >> "Bill Cunningham"<nos...@nspam.invalid> writes: > >>> pete wrote: > >>>> return *aa> *bb ? -1 : *aa != *bb; > > >>> Can you show me that for loop that's not in shorthand? This is > >>> kind of hard to read. > > >> What for loop? > > > Ok I saw the ?: and thought it might be part of for. > > Doesn't a for loop normally include the word "for"? does it normally include a "?"?
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2011-11-12 22:42 -0500 |
| Message-ID | <4EBF3C93.11EB@mindspring.com> |
| In reply to | #13218 |
Bill Cunningham wrote:
>
> pete wrote:
> > return *aa > *bb ? -1 : *aa != *bb;
>
> Can you show me that for loop that's not in shorthand?
> This is kind of hard to read.
if (*aa > *bb) {
return -1;
} else {
if (*aa != *bb) {
return 1;
} else {
return 0;
}
}
--
pete
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web