Link to home
Start Free TrialLog in
Avatar of xinex
xinex

asked on

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.

Avatar of sunnycoder
sunnycoder
Flag of India image

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

}
xinex, pls provide some feedback to the experts who respond to your question.
if your questions have been answered then close them
https://www.experts-exchange.com/help/closing.jsp
otherwise post your feedback
Avatar of n_fortynine
n_fortynine

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


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
ASKER CERTIFIED SOLUTION
Avatar of Sys_Prog
Sys_Prog
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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. :)
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 don't think u can return 3 distinct values using ?:

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

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

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

>  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


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......??
Read my previous post. I-t-'s-a-m-a-t-t-e-r-o-f-s-t-y-l-e.
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.
>>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.