Jump to content
  • Advertisement
Sign in to follow this  
Shuger

qsort from stdlib.usage

This topic is 4239 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone! I have run into a problem i can't solve. I need to use qsort function from standard c library, but i don't know how to pass a comparing function to it, and how this function should look like, i tried many sites but they don't describe it clearly enough for me.

Share this post


Link to post
Share on other sites
Advertisement
The first result from typing "qsort" into Google has a code sample:

/* qsort example */
#include <stdio.h>
#include <stdlib.h>

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}

int main ()
{
int * pItem;
int n;
qsort (values, 6, sizeof(int), compare);
for (n=0; n<6; n++)
{
printf ("%d ",values[n]);
}
return 0;
}


Edit: Bah, too slow [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Shuger
I need to use qsort function from standard c library


Are you sure? (If working in C++, check std::sort, in <algorithm>. It's much more flexible, type-safe, easier to use, and according to Stroustrup, often better performing because the compiler is more able to inline the comparisons.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!