P
pereges
It is not explained well in my manual. can someone please tell me
about it ?
Is qsort the best sorting algorithm out there ?
about it ?
Is qsort the best sorting algorithm out there ?
It is not explained well in my manual. can someone please tell me
about it ?
Is qsort the best sorting algorithm out there ?
pereges said:It is not explained well in my manual. can someone please tell me
about it ?
Is qsort the best sorting algorithm out there ?
pereges said:It is not explained well in my manual. can someone please tell me
about it ?
Is qsort the best sorting algorithm out there ?
'it' is a pronoun, normally used to refer to a noun used earlier in
the paragraph.
pereges said:I think in this case I will be better of writing an sorting function
of my own. my array is a buffer of vertices where each vertex is
actually a struct having a x, y and z component. i need to sort the
records according to x coordinate or y coordinate or z coordinate. i
don't think you can use qsort to accomplish this.
Well, actually you can, provided that you can define an ordering
relationship between two vertices. For example, let us say that we want to
order by x first, by y if x1==x2, and by z if x1==x2 and y1==y2. We do it
like this:
pereges said:What is this ordering relationship ? Why should we sort along y if x1
== x2 or z if x1 == x2 and
y1 ==y2 ?
kdroot->bhead = calloc(sizeof(bsphere), o->ntri);maxB.x, kdroot->maxB.y, kdroot->maxB.z, kdroot->minB.x, kdroot-
minB.y, kdroot->minB.z); */
vert[o->tri.v0], o->vert[o->tri.v1], o->vert[o->tri.v2]);
bhead.cen.x, kdroot->bhead.cen.y, kdroot->bhead.cen.z,
maxB.x, kdroot->maxB.y, kdroot->maxB.z, kdroot->minB.x, kdroot-
minB.y, kdroot->minB.z); */
vert[o->tri.v0], o->vert[o->tri.v1], o->vert[o->tri.v2]);
bhead.cen.z,kdroot->bhead.r);*/
bhead.cen.x. I wonder if something like below would work -
This is too complicated. I have to sort the array of structures given
by kd->bhead pointer according to the x coordinate of center. kd-bhead.cen.x. I wonder if something like below would work -
void cmpcenter_x(const void *cpa, const void *cpb)
{
const bsphere *ca = cpa;
const bsphere *cb = cpb;
return(ca->cen.x - cb.cen.x);
}
pereges said:This is too complicated. I have to sort the array of structures given
by kd->bhead pointer according to the x coordinate of center. kd-bhead.cen.x. I wonder if something like below would work -
void cmpcenter_x(const void *cpa, const void *cpb)
{
const bsphere *ca = cpa;
const bsphere *cb = cpb;
return(ca->cen.x - cb.cen.x);
}
..................
..................
qsort(kd->bhead, kd->maxspheres, sizeof(bsphere), cmpcenter_x);
Using a subtraction like that:
return ca->cen.x - cb->cen.x;
is a cute trick, but you should avoid it unless you're *certain* that
it can't overflow (or wrap around if x is unsigned).
A more straightforward test is:
if (ca->cen.x < cb->cen.x) {
return -1;
}
else if (ca->cen.x > cb->cen.x) {
return 1;
}
else {
return 0;
}
Or, if you like terseness:
return ca->cen.x < cb->cen.x ? -1 : ca->cen.x > cb->cen.x;
(This takes advantage of the fact that ">" yields 0 or 1.)
Using a subtraction like that:
return ca->cen.x - cb->cen.x;
is a cute trick, but you should avoid it unless you're *certain* that
it can't overflow (or wrap around if x is unsigned).
A more straightforward test is:
if (ca->cen.x < cb->cen.x) {
return -1;
}
else if (ca->cen.x > cb->cen.x) {
return 1;
}
else {
return 0;
}
Or, if you like terseness:
return ca->cen.x < cb->cen.x ? -1 : ca->cen.x > cb->cen.x;
(This takes advantage of the fact that ">" yields 0 or 1.)
I don't think it will work with kdnode->bhead.cen.x
You are trying to sort an array bhead[0..maxspheres] according to x of
center element. Probably, qsort will not give results with comparison
functions like the one above.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.