473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Qsort inaccuracy?

Hi All:

The code below (using the qsort function) produces the following
incorrect result. The last two numbers are not sorted. It this
innaccurate result specific to my compiler's qsort, or is there a bug
in my code? Thanks...

Original Array:
3.125420
8.618710
4.220840
2.181950
8.852060
1.763020
0.164010

Sorted Array:
0.164010
1.763020
2.181950
3.125420
4.220840
8.852060
8.618710

------------------------------------------

#include <stdio.h>
#include <stdlib.h>

typedef int (*qsortfunc) ( const void*, const void* );
int
compare_doubles (const double *a, const double *b)
{
return (int) (*a - *b);
}

int main ()
{

int i;
double array[7];
array[0] = 3.12542;
array[1] = 8.61871;
array[2] = 4.22084;
array[3] = 2.18195;
array[4] = 8.85206;
array[5] = 1.76302;
array[6] = 0.16401;

printf ("\nOriginal Array:\n");

for (i = 0; i < 7; i++)
printf ("%lf\n", array[i]);

qsort(array, 7, sizeof(double),(qsortfunc) compare_doubles);

printf ("\nSorted Array:\n");
for (i = 0; i < 7; i++)
printf ("%lf\n", array[i]);
return 1;
}

Nov 14 '05 #1
3 2278
On Tue, 14 Dec 2004 08:04:10 -0800, No Such Luck wrote:
Hi All:

The code below (using the qsort function) produces the following
incorrect result. The last two numbers are not sorted. It this
innaccurate result specific to my compiler's qsort, or is there a bug
in my code? Thanks...

Original Array:
3.125420
8.618710
4.220840
2.181950
8.852060
1.763020
0.164010

Sorted Array:
0.164010
1.763020
2.181950
3.125420
4.220840
8.852060
8.618710

------------------------------------------

#include <stdio.h>
#include <stdlib.h>

typedef int (*qsortfunc) ( const void*, const void* );
Get rid of that, you don't need it.
int
compare_doubles (const double *a, const double *b)
A qsort() comparison fuction is required to have 2 parameters of type
const void *. The function you have defined here is not compatible with
this and is invalid. The fact that you had to use a cast in the call to
qsort() below should ring alarm bells. Consider that qsort() will be
passing const void * arguments to the comparison function but
const double * may not have the same representation.
{
return (int) (*a - *b);
This is not valid. For example (int)(8.85206-8.61871) will evaluate to
zero but the numbers are not equal. Also consider what happens if the
difference is too big to fit in an int.
}
You need something like

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

if (da == db)
return 0;

return (da > db) ? 1 : -1;
}

int main ()
{

int i;
double array[7];
array[0] = 3.12542;
array[1] = 8.61871;
array[2] = 4.22084;
array[3] = 2.18195;
array[4] = 8.85206;
array[5] = 1.76302;
array[6] = 0.16401;

printf ("\nOriginal Array:\n");

for (i = 0; i < 7; i++)
printf ("%lf\n", array[i]);
The normal printf() conversion specifier for double is %f, C99 added
support for %lf too, but %f also works with C90 which is still in common
use.

qsort(array, 7, sizeof(double),(qsortfunc) compare_doubles);
qsort(array, 7, sizeof *array, compare_doubles);

You don't need the cast, and this form of the 3rd argument works even if
you changed the type of array.
printf ("\nSorted Array:\n");
for (i = 0; i < 7; i++)
printf ("%lf\n", array[i]);
Again %f is better.

return 1;
}


Portable return values from the initial invocation of main() are 0 and
EXIT_SUCCESS which indicate success and EXUT_FAILURE which indicates
failure. The last 2 are defined in <stdlib.h>.

Lawrence
Nov 14 '05 #2
On Tue, 14 Dec 2004 16:57:46 +0000, Lawrence Kirby wrote:

....
Portable return values from the initial invocation of main() are 0 and
EXIT_SUCCESS which indicate success and EXUT_FAILURE which indicates
failure. The last 2 are defined in <stdlib.h>.


Sorry, make that EXIT_FAILURE

Lawrence

Nov 14 '05 #3


No Such Luck wrote:
Hi All:

The code below (using the qsort function) produces the following
incorrect result. The last two numbers are not sorted. It this
innaccurate result specific to my compiler's qsort, or is there a bug
in my code?
Yes.
The compare function is flawed. See below.
Original Array:
3.125420
8.618710
4.220840
2.181950
8.852060
1.763020
0.164010

Sorted Array:
0.164010
1.763020
2.181950
3.125420
4.220840
8.852060
8.618710

------------------------------------------

#include <stdio.h>
#include <stdlib.h>

typedef int (*qsortfunc) ( const void*, const void* );
int
compare_doubles (const double *a, const double *b)
{
return (int) (*a - *b);
}
1. The return statement is wrong. For example, with
(int)(8.852060 - 8.618710), the cast to int results in a
return of 0. 0 indicates to function qsort that the two
values are equal. As you see, they are not equal.

2. The prototype for the comparison function is:
int cmp(const void *, const void *);

You need to remove the typedef.
Change the compare_doubles function to something like:

int compare_doubles (const void *v1, const void *v2)
{
const double *d1 = v1;
const double *d2 = v2;

return (*d1 < *d2)?-1:(*d1!=*d2);
}

And remove the qsortfunc cast in the qsort statement.
int main ()
{

int i;
double array[7];
array[0] = 3.12542;
array[1] = 8.61871;
array[2] = 4.22084;
array[3] = 2.18195;
array[4] = 8.85206;
array[5] = 1.76302;
array[6] = 0.16401;

printf ("\nOriginal Array:\n");

for (i = 0; i < 7; i++)
printf ("%lf\n", array[i]);

qsort(array, 7, sizeof(double),(qsortfunc) compare_doubles);

printf ("\nSorted Array:\n");
for (i = 0; i < 7; i++)
printf ("%lf\n", array[i]);
return 1;
}


--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: richard | last post by:
What might cause qsort to crash? I'm using qsort to sort a few thousand items of a not too complex structure. Tried it with one data set - worked fine. Tried another similarly sized data set...
11
by: William Buch | last post by:
I have a strange problem. The code isn't written by me, but uses the qsort function in stdlib. ALWAYS, the fourth time through, the memory location of variable list (i.e. mem location = 41813698)...
5
by: Steve | last post by:
can someone tell me how qsort function in <stdlib.h> is used (qsort(..........))? the function has a buffer, two void * parameters and the a pointer to a compare function. Thanks.
7
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
17
by: Trent Buck | last post by:
The fourth argument is a comparator that returns `an integer less than, equal to, or greater than zero' depending on the ordering of its arguments. If I don't care about the order and simply...
32
by: John Smith | last post by:
I'm trying to figure out qsort(). I haven't seen any practical examples, only synopsis. In the code below, the array is not sorted. Can someone give me some help? #include <stdio.h> #include...
10
by: No Such Luck | last post by:
Hi All: The code below (using the qsort function) produces the following incorrect result. The last two numbers are not sorted. It this innaccurate result specific to my compiler's qsort, or is...
10
by: gauss010 | last post by:
Suppose I have an object A of type char. Each A is a buffer containing a string, and I want to sort the M strings of A using the strcmp function. The description of the qsort function says that I...
61
by: Ron Ford | last post by:
K&R has three different versions of qsort, and the ultimate one is supposed to be like the one in the std library. I'm trying to implement the first, which is in §4.10. I think I'm pretty close...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.