• C

# array sort

how can i sort a small array with using standard c function qsort(); with minum code.

int arr[20]; //this array contain only number.

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
int compare ( const void * a, const void * b )
{
int * p = a;
int * q = b;

if ( *p > *q )
return 1;

if ( *p < *q )
return -1;

if ( *p == *q )
return 0;
}

qsort ( arr, 20, sizeof(int), compare);
Commented:
#include <stdio.h>

int compare_doubles (const void *a, const void *b)
{
const int *da = (const int *) a;
const int *db = (const int *) b;

return (*da > *db) - (*da < *db);
}

int main(void) {
int i ;
int a[5] = {1,3,2,6,4} ;

qsort ( a, 5, sizeof ( int ), compare_doubles ) ;
for ( i = 0; i < 5; i++ )
printf ( "%d", a[i] ) ;
system ( "PAUSE" ) ;
return 0;

}
Commented:
xinex, pls provide some feedback to the experts who respond to your question.
http://www.experts-exchange.com/help/closing.jsp
Commented:
you can shorten the compare function by making it just const int* instead of const void*
int compare (const int * a, const int * b ) {
return *a > *b ? 1 : *a < *b ? -1 : 0;
}

and cast it when you call qsort
qsort(a, NumberOfElems, sizeof(a[0]), (int (*)(const void *,const void*))compare);
DBACommented:

Hi n_fortynine,

You're right.  The code can be made shorter.

It's a matter of style.  In this case I'll agree with Sunnycoder.  The code's a lot cleaner if the casting is already established when you call qsort().

If you really want to shorten compare(), return the arithmetic value instead of the logical one.

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

Though this does tend to break down when (a) and (b) are near their limits.  (compare (-119, 120) should return 1, but using 8-bit ints to demonstrate the limit problem it's clear that an overflow caused by the calculation (120 - (-119)) can return a negative value.  239 is an 0xEF.  Since the sign bit would be set if this were an 8-bit int, the returned value is actually -16.)

Kent
Commented:
Hey Hey n_fortynine,

I think the compare function should be a generic one and should not make the caller of the function cast something. It's an internal operation, so I think it should be done in the compare function itself

And regarding clearness of code, I think he has asked for a shorter code, so that's the reason I have done that logical value return

Regarding Kdo's code,

I think our coding should not fail for any case, it should handle any condition. And as u said, your code won't take care of some conditions

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
>>I think the compare function should be a generic one and should not make the
>>caller of the function cast something. It's an internal operation, so I think it
>>should be done in the compare function itself

Depends on what you want. If you cast it, the only thing that will suffer is your compiler, but that will happen during compile time. Otherwise, you're adding *unnecessary* instructions to your compare function and you will see that when it runs.

And yes it's a matter of style. :)
Commented:
wont the size of code actually generated by the compiler be the same except for Kents code which will be shorter
putting ?: in place of if then else wont exactly reduce the size... clarity may be ;o)
Commented:
I don't think u can return 3 distinct values using ?:

Commented:
>>wont the size of code actually generated by the compiler be the same except >>for Kents code which will be shorter
>>putting ?: in place of if then else wont exactly reduce the size... clarity may be ;o)

I'm not talking about using ?:, but the casting part. Yes, Kents code would be shorter.

>>I don't think u can return 3 distinct values using ?:
In C? You can do anything in C :) lol
Commented:

====="In C? You can do anything in C :) lol "===========

In that case, it would involve a cumbersome syntax--------clarity......??
DBACommented:

>  I don't think u can return 3 distinct values using ?:

return (a < b ? -1 : a == b ? 0 : 1);

As I recall this does require fewer instructions that the if/then/else equivalent.

Kent

Commented:
xinex, can you please give feedback for the replies, so that we don't have to carry on these silly arguments anymore?

>>In that case, it would involve a cumbersome syntax--------clarity......??
I'm merely making a suggestion here. Note that my suggestion wasn't that he (xinex) should use that my style of code (which I think in this situation provides enough readbility for a simple statement, but anyway...), but that he could cast the function instead of having to do
const int *da = (const int *) a;
const int *db = (const int *) b;

I'm sorry if I confused some of you.
Commented:
>>In that case, it would involve a cumbersome syntax--------clarity......??

The questioner did ask for minimum code.

When coding you often have to make choices between clarity and minimum code.
In this question, the questioner is looking for minimum code over clarity.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.