快速排序 [英] qsort

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

问题描述

我正在试图找出qsort()。我还没有看到任何实际的例子,

只是故事大纲。在下面的代码中,数组未排序。有人可以给我一些帮助吗?


#include< stdio.h>

#include< stdlib.h> ;

int compare(const void * a,const void * b);


int main(void)

{

int idx;

int array [] = {243,12,99,106,122,77,242};


qsort(array,7,4,& compare);

for(idx = 0; idx< 7; ++ idx)

printf("%d \t",array [idx]);

printf(" \ n");


返回0;

}

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

{

if(a< b)返回-1;

如果(a == b)返回0;

如果(a> b)返回1;

}

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 <stdlib.h>
int compare(const void* a, const void* b);

int main(void)
{
int idx;
int array[] = {243, 12, 99, 106, 122, 77, 242};

qsort(array, 7, 4, &compare);
for(idx=0; idx<7; ++idx)
printf("%d\t", array[idx]);
printf("\n");

return 0;
}

int compare(const void* a, const void* b)
{
if(a < b) return -1;
if(a == b) return 0;
if(a > b) return 1;
}

推荐答案

John Smith写道:
John Smith wrote:
我正在试图找出qsort()。我还没有看到任何实际的例子,
只是故事大纲。在下面的代码中,数组未排序。有人可以给我一些帮助吗?

#include< stdio.h>
#include< stdlib.h>
int compare(const void * a ,const void * b);

int main(void)
{id / int intx;
int array [] = {243,12,99,106, 122,77,242};

qsort(数组,7,4和&比较);


&是无害的,但不必要。对于你的C实现,'4''可能是
,但不是通用的:

每个C实现选择自己的最佳实现。

`int''的大小,不同的实现选择不同。

为了便于携带,请改写`sizeof array [0]'。

for(idx = 0; idx< 7; ++ idx)
printf("%d \t",array [idx]);
printf(" \ n");

返回0;
}
int比较(const void * a,const void * b)
{
if(a< a< ; b)返回-1;
如果(a == b)返回0;
如果(a> b)返回1;
}
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 <stdlib.h>
int compare(const void* a, const void* b);

int main(void)
{
int idx;
int array[] = {243, 12, 99, 106, 122, 77, 242};

qsort(array, 7, 4, &compare);
The `&'' is harmless, but unnecessary. The `4'' may
be true for your C implementation, but is not universal:
each C implementation chooses its own "best" size for
`int'', and different implementations choose differently.
For portability, write `sizeof array[0]'' instead.
for(idx=0; idx<7; ++idx)
printf("%d\t", array[idx]);
printf("\n");

return 0;
}

int compare(const void* a, const void* b)
{
if(a < b) return -1;
if(a == b) return 0;
if(a > b) return 1;
}




这是你的难度。比较函数's
参数不是两个数组元素,而是指向两个数组元素的
。您正在比较指针,但是您希望比较指向对象的
。这是一个

的方法:


int compare(const void * a,const void * b){

int u = *(const int *)a;

int v = *(const int *)b;

if(u< v)...


-
Er ********* @ sun.com



Here''s your difficulty. The comparison function''s
arguments are not two array elements, but pointers to
two array elements. You are comparing the pointers, but
you want to compare the pointed-to objects. Here is one
way to do it:

int compare(const void *a, const void *b) {
int u = *(const int*)a;
int v = *(const int*)b;
if (u < v) ...

--
Er*********@sun.com


Quoth John Smith on the about 2004-11-17:
Quoth John Smith on or about 2004-11-17:
if(a< ; b)返回-1;
如果(a == b)返回0;
如果(a> b)返回1;
if(a < b) return -1;
if(a == b) return 0;
if(a > b) return 1;




首先,不应该取消引用它们吗?



First of all, shouldn''t these be dereferenced?


John Smith写道:
John Smith wrote:
我正在试图找出qsort( )。我还没有看到任何实际的例子,
只是故事大纲。在下面的代码中,数组未排序。有人可以给我一些帮助吗?

#include< stdio.h>
#include< stdlib.h>
int compare(const void * a ,const void * b);

int main(void)
{id / int intx;
int array [] = {243,12,99,106, 122,77,242};

qsort(数组,7,4和&比较);
for(idx = 0; idx< 7; ++ idx)
printf("%d \t",array [idx]);
printf(" \ n");

返回0;
}

int compare(const void * a,const void * b)
{
if(a< b)return -1;
if(a == b)return 0;
if(a> b)返回1;
}
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 <stdlib.h>
int compare(const void* a, const void* b);

int main(void)
{
int idx;
int array[] = {243, 12, 99, 106, 122, 77, 242};

qsort(array, 7, 4, &compare);
for(idx=0; idx<7; ++idx)
printf("%d\t", array[idx]);
printf("\n");

return 0;
}

int compare(const void* a, const void* b)
{
if(a < b) return -1;
if(a == b) return 0;
if(a > b) return 1;
}




在''compare''实现中,你正在比较指针

比较这些指针指向的值。你应该做后者,而不是前者。
。例如,你可以这样做


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

{

int ia = *(const int *)a;

int ib = *(const int *)b;

return(ia> ib) - (ia< ; ib);

}


此外,使用

形成''qsort''的参数更有意义''sizeof'',而不是明确指定具体值


qsort(数组,sizeof数组/ sizeof *数组,sizeof *数组,& compare);


-

祝你好运,

Andrey Tarasevich



Inside your ''compare'' implementation you are comparing pointers instead
of comparing the values pointed by those pointers. You are supposed to
do the latter, not the former. For example, you can do it as follows

int compare(const void* a, const void* b)
{
int ia = *(const int*) a;
int ib = *(const int*) b;
return (ia > ib) - (ia < ib);
}

Also, it makes more sense to form arguments of ''qsort'' by using
''sizeof'', instead of specifying concrete values explicitly

qsort(array, sizeof array / sizeof *array, sizeof *array, &compare);

--
Best regards,
Andrey Tarasevich


这篇关于快速排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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