Qsort不准确? [英] Qsort inaccuracy?

查看:70
本文介绍了Qsort不准确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:


下面的代码(使用qsort函数)产生以下

不正确的结果。最后两个数字未排序。这个

特定于我编译器的qsort的无效结果,还是我的代码中有一个错误

?谢谢...


原始数组:

3.125420

8.618710

4.220840

2.181950

8.852060

1.763020

0.164010


分类数组:

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

数组[0] = 3.12542;

数组[1] = 8.61871;

数组[2] = 4.22084;

array [3] = 2.18195;

array [4] = 8.85206;

array [5] = 1.76302;

array [6] = 0.16401;


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


for(i = 0;我< 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]);

返回1;

}

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

推荐答案

2004年12月14日星期二08:04:10 -0800,No Such Luck写道:
On Tue, 14 Dec 2004 08:04:10 -0800, No Such Luck wrote:
大家好:

下面的代码(使用qsort函数)会产生以下不正确的结果。最后两个数字未排序。这个
不确定的结果特定于我的编译器的qsort,或者我的代码中是否有错误?谢谢...

原文数据:
3.125420
8.618710
4.220840
2.181950
8.852060
1.763020
0.164010

分类阵列:
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)


一个qsort()比较函数需要有2个类型的参数

const void *。您在此处定义的功能与

不兼容,并且无效。你必须在下面的

qsort()调用中使用强制转换的事实应该敲响警钟。考虑到qsort()将

传递给const void *参数到比较函数但是

const double *可能没有相同的表示。

{
return(int)(* a - * b);


这是无效的。例如(int)(8.85206-8.61871)将评估为
零,但数字不相等。还要考虑如果

的差异太大而不适合int,会发生什么。

}


你需要像


int compare_doubles(const void * a,const void * b)

{

double da = *(const double *) a;

double db = *(const double *)b;


if(da == db)

返回0 ;


返回(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(" \\\
Original Array: (i = 0; i< 7; i ++)
printf("%lf \ n",array [i]); \\ n \\ n;


double的正常printf()转换说明符是%f,C99添加
$ l $ b支持%lf,但%f也适用于C90,这是仍然是共同的

使用。

qsort(数组,7,sizeof(double),(qsortfunc)compare_doubles);


qsort(数组,7,sizeof *数组,compare_doubles);


你不需要演员,这种形式的即使你改变了数组的类型,第三个参数也有效。

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


再次%f更好。

返回1;
}
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;
}




初始调用main()的便携式返回值为0和

EXIT_SUCCESS表示成功,EXUT_FAILURE表示

失败。最后2个在< stdlib.h>中定义。


劳伦斯



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


2004年12月14日星期二16:57 :46 +0000,Lawrence Kirby写道:


....
On Tue, 14 Dec 2004 16:57:46 +0000, Lawrence Kirby wrote:

....
main()初始调用的便携式返回值为0和
EXIT_SUCCESS表示成功,EXUT_FAILURE表示
失败。最后2个在< stdlib.h>中定义。
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>.




对不起,让那个EXIT_FAILURE


劳伦斯





No Such Luck写道:


No Such Luck wrote:
大家好:
不确定的结果特定于我的编译器的qsort,或者我的代码中是否有错误?


是。

比较功能有缺陷。见下文。

原始数组:
3.125420
8.618710
4.220840
2.181950
8.852060
1.763020
0.164010 <分类数组:
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.退货声明错误。例如,使用

(int)(8.852060 - 8.618710),转换为int导致

返回0. 0表示函数qsort表示两个

值相等。如你所见,它们并不相等。


2.比较函数的原型是:

int cmp(const void *,const void *) ;


您需要删除typedef。

将compare_doubles函数更改为:


int compare_doubles (const void * v1,const void * v2)

{

const double * d1 = v1;

const double * d2 = v2;


返回(* d1< * d2)? - 1:(* d1!= * d2);

}

并删除qsort语句中的qsortfunc强制转换。

int main()

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

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

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

qsort(array,7,sizeof(do uble),(qsortfunc)compare_doubles);

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

返回1;
}
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

美国佛罗里达州坦帕市

mailto: xa ****** @ myrapidsys.com (删除x发送电子邮件)
http://www.geocities.com/abowers822/



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


这篇关于Qsort不准确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆