Solved

# array sort

Posted on 2003-10-22
235 Views
how can i sort a small array with using standard c function qsort(); with minum code.

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

0
Question by:xinex
• 4
• 4
• 3
• +2

LVL 45

Expert Comment

ID: 9598574
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);
0

LVL 10

Expert Comment

ID: 9598579
#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;

}
0

LVL 45

Expert Comment

ID: 9598636
xinex, pls provide some feedback to the experts who respond to your question.
if your questions have been answered then close them
http://www.experts-exchange.com/help/closing.jsp
otherwise post your feedback
0

LVL 4

Expert Comment

ID: 9599713
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);
0

LVL 45

Expert Comment

ID: 9599890

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
0

LVL 10

Accepted Solution

Sys_Prog earned 100 total points
ID: 9604351
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

0

LVL 4

Expert Comment

ID: 9604406
>>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. :)
0

LVL 45

Expert Comment

ID: 9604446
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)
0

LVL 10

Expert Comment

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

0

LVL 4

Expert Comment

ID: 9604520
>>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
0

LVL 10

Expert Comment

ID: 9604633

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

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

LVL 45

Expert Comment

ID: 9605781

>  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

0

LVL 4

Expert Comment

ID: 9607065
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.
0

LVL 30

Expert Comment

ID: 9614180
>>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.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

#### 809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.