look for qsort() -- all you have to do is write your own custom comparison function that returns an integer similar to strcmp(). The code below assums you are sorting an int array in descending order.
int mycomp(const void* p1, const void* p2)
{
int* i1 = (int*)p1;
int* i2 = (int *)p2;
return *i2 - *i1;
}
Or an array of strings, in descending order (I think)
int mycomp(const void* p1, const void* p2)
{
int x = strcmp((char*)p1,(char*)p2);
return (x < 0) ? 1 : (x > 0) ? -1 : 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
AD: There's no need to cast away constness (it may do no harm, but I avoid things that can be bad habits). And your integer comparison function can be a victim of integer overflow.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>return *i2 - *i1;
Eew. Can we say integer overflow?
>return (x < 0) ? 1 : (x > 0) ? -1 : 0;
Eeeeew! Nested conditional statements are fugly and hard to follow. How about this:
int compare ( const void *a, const void *b )
{
return -strcmp ( (const char *)a, (const char *)b );
}
;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
now how can subtracting two integers possibly cause integer overflow?
Narue: I agree your solution is simpler. But don't thumb you nose at nested conditional statements just because you may not understand them. Eeeeew :lol:
Dave: I'm sertain you already know that the two parameters must be cast to the correct type. So I don't know what you meant in your post. I could have posted this, but others, such as Narue, may not have understood it
return *(int *)p2 - *(int *)p1;
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
now how can subtracting two integers possibly cause integer overflow?
What's INT_MAX - INT_MIN?Dave: I'm sertain you already know that the two parameters must be cast to the correct type.Right, you are incorrectly casting away the constness. Casting with (const int*) would be better.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>now how can subtracting two integers possibly cause integer overflow?
Under is over and over is under. :) The point is that you could subtract beyond the limits of a signed integer, and Bad Things(TM) happen when you do stuff like that.
>But don't thumb you nose at nested conditional statements just
>because you may not understand them.
If everyone thumbed their noses at nested conditional statements, I wouldn't need to understand them. And the world would be a better place. ;)
>I'm sertain you already know that the two parameters must be cast to the correct type.
And const is there for a reason. But for assignment there's no need to use casts:
int compare ( const void *a, const void *b )
{
const int *ia = a;
const int *ib = b;
if ( *ia < *ib )
return +1;
else if ( *ia > *ib )
return -1;
else
return 0;
}
When you go directly into a function call, a cast is a good idea, but remember that strcmp expects pointers to const char, not pointers to char. You don't cast to the actual type, you cast to the expected type and hope with all your might that the two types are compatible. :)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
What's INT_MAX - INT_MIN?
-1 with my compiler -- but you'r right about int overvlow in that one and only case. I don't ever recall any number of real world applications where that will happen, but then I havn't read all real-world programs either :DRight, you are incorrectly casting away the constness. Casting with (const int*) would be better.
Ok now I see your objection. Easy fix to the purests, but does not really do anything in this situation. But I suppose one should be consistent and use const correctly as you pointed out.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
but remember that strcmp expects pointers to const char, not pointers to char.
that's one of the purposes of function prototypes, to give the compiler enough information to make correct data type conversions. So you always do this? (I don't)
char name1[20];
char name2[20];
...
if( strcmp( (const char*)name1, (const char*)name2))
...
I never explicitly typecast the parameters to const char*. So why bother in that comparison function I posted ?
And the same thing applies to all functions that take a const something-or-another.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
-1 with my compiler
No, but that's what it does with the undefined behavior. (And can you guess how that might screw up a sort?)but you'r right about int overvlow in that one and only case.Okay, how about subtracting from INT_MAX any negative number? For me that gives a couple billion cases. Then take INT_MAX-1 and a couple billion minus one more. Then take INT_MAX-2 ...
And you could also start on the other side: Subtract from INT_MIN and positive number. Subtract from INT_MIN+1 every number greater than 2.
I'd say there are quite a few combinations of two integers that result in undefined behavior.
[edit]By the way, this is a comparison function I'd run across.
int bar(const void *a, const void *b)
{
const int *x = a, *y = b;
return (*x > *y) - (*x < *y);
}
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Oh! your right Dave -- because subtracting a minus is really addition. I knew that, just checking if you did too :cheesy: :o
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
int bar(const void *a, const void *b)
{
const int *x = a, *y = b;
return (*x > *y) - (*x < *y);
}
That will not compile with either VC++ 6.0 or Dev-C++. Requires cast like this
int bar(const void *a, const void *b)
{
const int *x = (int*)a, *y = (int*)b;
return (*x > *y) - (*x < *y);
}
but otherwise an interesting algorithm.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Avoid compiling C code in a C++ compiler. (Or else save the file with a .c extension instead of .cpp!)
[edit]but otherwise an interesting algorithm.Yeah, I've been trying to Google the thing about 'no branches' or 'no instruction flush' or whatever it was -- something potentially beneficial to remember so that I could Google it again one day.
[edit=2]Here's another integer compare function that Narue might enjoy.
int baz(const void *a, const void *b)
{
const int *x = a, *y = b;
return (*x < *y) ? -1 : (*x > *y);
}
[edit=3]And note that in all my posts to this thread, the comparison function when used with qsort would sort in increasing order -- rather than the OP's requested decreasing order.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>So why bother in that comparison function I posted ?
Because pointers to void can be funny beasties. I like to make sure that everything matches up before the function call on the off chance that I miss a subtle issue. That's actually a throwback from when I didn't fully understand pointers and wrote code to protect myself from my own ignorance, but at the very least it proves that I know what types strcmp expects. :)
>That will not compile with either VC++ 6.0 or Dev-C++. Requires cast like this
Try Turbo C, at least that way you won't accidentally compile as C++. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Why are you guys using void*, ewww on that. This is C++, use its power.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
>> is that right?
No. You have to use some if statements to check of the value of A > B, or B > C or C > A.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
To match your very little effort at asking the question..
my answer is: std::sort ( header).
mike_2000_17
Posting Virtuoso
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
any one can u plz tell me the code for sorting 10 numbers using C++ codes
put the numbers in an array then google for bubblesort.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343