Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   qsort (http://www.velocityreviews.com/forums/t805802-qsort.html)

 Bill Cunningham 11-11-2011 02:00 PM

qsort

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

 Malcolm McLean 11-11-2011 02:56 PM

Re: qsort

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);

 John Gordon 11-11-2011 03:20 PM

Re: qsort

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

--
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"

 Ben Bacarisse 11-11-2011 04:39 PM

Re: qsort

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.

 Bill Cunningham 11-11-2011 07:22 PM

Re: qsort

John Gordon wrote:

[snip]

> If you knew the parameter was for passing in a comparison function,
> why did

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

 88888 Dihedral 11-12-2011 06:19 PM

Re: qsort

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.

 Bill Cunningham 11-13-2011 12:10 AM

Re: qsort

pete wrote:
> return *aa > *bb ? -1 : *aa != *bb;

Can you show me that for loop that's not in shorthand? This is kind of

Bill

 Keith Thompson 11-13-2011 12:40 AM

Re: qsort

"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

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"

 Bill Cunningham 11-13-2011 06:39 AM

Re: qsort

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

 Ian Collins 11-13-2011 06:51 AM

Re: qsort

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

All times are GMT. The time now is 11:20 PM.