Solved

array sort

Posted on 2003-10-22
14
233 Views
Last Modified: 2012-05-04
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
Comment
Question by:xinex
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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

by:Sys_Prog
Comment Utility
#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

by:sunnycoder
Comment Utility
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

by:n_fortynine
Comment Utility
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

by:Kdo
Comment Utility


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

by:
Sys_Prog earned 100 total points
Comment Utility
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

by:n_fortynine
Comment Utility
>>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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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

by:Sys_Prog
Comment Utility
I don't think u can return 3 distinct values using ?:

0
 
LVL 4

Expert Comment

by:n_fortynine
Comment Utility
>>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

by:Sys_Prog
Comment Utility

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

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

Expert Comment

by:Kdo
Comment Utility

>  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

by:n_fortynine
Comment Utility
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

by:Axter
Comment Utility
>>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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Absolute madness with managing time in C 4 80
Details to do the search 56 141
Way to improve it 16 59
Console based application in Linux 1 75
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 recursion in the C programming language.

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

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now