You're not stupid, just uneducated -- or probably forgot everything you
learned in algorithms class 60 seconds after turning in the final :-).

You understand a basic bubble sort, right?...

for(i=0; i<N; i++)
 for(j=i+1; j<=N; j++)
   if value(i) > value(j) exchange i and j

So it starts and compares every item, exchanging them until the smallest
value is in the 0th slot.  Then it checks all the rest of the slots for the
2nd smallest item, etc.

qsort doesn't work that way.  qsort partitions the items into, well,
partitions, and sorts the sub partitions.  If the comparision fails because
of a null, then the partitions are wrong and when it puts them back
together, chunks are out of sequence.  So even though it later will ignore
these records, they're causing sorting problems.

Even if we used a slower bubble sort, we still have the problem, because of
the way our compare function treats missing data.

Remember our compare function:

    if((*a)->hostSymIpAddress[0] != '\0') {
      char *name1, *name2;

      name1 = (*a)->hostSymIpAddress;
      name2 = (*b)->hostSymIpAddress;

      rc = strcasecmp(name1, name2);
    } else
      rc = strcasecmp((*a)->ethAddressString, (*b)->ethAddressString);


Say you have 4 items (primary=hostSymIpAddress, secondary=ethAddressString)
and let's play bubble sort...

item primary   backup
a    \0         minee
b    0          moe
c    1          eenie
d    2          meenie

You all expect the sort to give b, c, d, a or a, b, c, d or maybe eenie,
meenie, minee, moe...  but because the comparision is not stable (that is a
? b is not the same as b ? a), it doesn't work...

1. a ? b   a=\0 so we do the backup test.  minee < moe, no exchange
2. a ? c   a=\0 so we do the backup test.  minee !< eenie, exchange

item primary   backup
c    1          eenie
b    0          moe
a    \0         minee
d    2          meenie

3. c ? d   c!=\0 so we do primary test. 1 < 2, no exchange

4. b ? a   now we did a ? b above, but ...
           b!=\0, so we do PRIMARY test.  moe !< minee, exchange

item primary   backup
c    1          eenie
a    \0         minee
b    0          moe
d    2          meenie

5. a ? d   a=\0, backup test, minee !< meenie, exchange

item primary   backup
c    1          eenie
d    2          meenie
b    0          moe
a    \0         minee

6. b ? a   same as last time, exchange

item primary   backup
c    1          eenie
d    2          meenie
a    \0         minee
b    0          moe


And the results are NOTHING like you expect...

With a qsort() it's worse, because once things get in the wrong partition,
they NEVER get back where they belong.  With a bubble sort, it's possible...




Google around for quicksort - these are some starting points

http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
http://www.dcs.ed.ac.uk/teaching/cs2/online/Lectures/CS2Bh/ADS/ads8.pdf

If you understand qsort, you'll see why it's MORE susceptible to the
problem.

-----Burton


> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Mark
> Gibbons
> Sent: Wednesday, February 18, 2004 3:17 PM
> To: [EMAIL PROTECTED]
> Subject: RE: [Ntop] Sorting
>
>
> Yes,  I can see the missing data......
>
> BUT I must be stupid or something ...
>
> The problem is with the PRESENTED data.  All the rows displayed
> have valid data whether it be IP or resolved host - There are no
> nulls in the presented hosts column - why is the presented data
> in the host column NOT the data that is used in the comparative
> sort?  As I understand it you are sorting on a different column
> than the one that is actually displayed - this makes no sense to
> me but then again there are many things that don't :-)
>
> Thanks for your patience.
>
> Mark
>
>
>
> -----Original Message-----
> From: Burton M. Strauss III [mailto:[EMAIL PROTECTED]
> Sent: 18 February 2004 20:36
> To: [EMAIL PROTECTED]
> Subject: RE: [Ntop] Sorting
>
>
> > -----Original Message-----
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Mark
> > Gibbons
> > Sent: Wednesday, February 18, 2004 2:07 PM
> > To: [EMAIL PROTECTED]
> > Subject: RE: [Ntop] Sorting
> >
> >
> > Herewith Data Dump:
> >
> >
> > key|index|hostNumIpAddress|hostSymIpAddress|firstSeen|lastSeen|min
> <snip/>
> > 10.11.16.92|0|10.11.16.92||1077111794|1077112394|31|127|3443|5886|
>                            ^^
>
> See the adjacent ||s for hostSymIpAddress??
>
> Those are the nulls (unresolved or unable to resolve) I was
> talking about...
>
> Once you feed a qsort a bad comparision, ghu only knows what the sort will
> look like!
>
> -----Burton
>

_______________________________________________
Ntop mailing list
[EMAIL PROTECTED]
http://listgateway.unipi.it/mailman/listinfo/ntop

Reply via email to