home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / programm / 3571 < prev    next >
Encoding:
Internet Message Format  |  1993-01-24  |  1.4 KB

  1. Path: sparky!uunet!ukma!usenet.ins.cwru.edu!b63519.STUDENT.CWRU.Edu!dpn2
  2. From: dpn2@po.CWRU.Edu (Damien P. Neil)
  3. Newsgroups: comp.programming
  4. Subject: Re: is it possible to find max & next smallest in one pass?
  5. Date: Sun, 24 Jan 1993 18:42:05 GMT
  6. Organization: Case Western Reserve University
  7. Lines: 32
  8. Message-ID: <dpn2.234.727900925@po.CWRU.Edu>
  9. References: <1993Jan24.040702.6193@emr1.emr.ca> <51915@seismo.CSS.GOV> <hjv.727890482@groucho.phil.ruu.nl>
  10. NNTP-Posting-Host: b63519.student.cwru.edu
  11.  
  12. In article <hjv.727890482@groucho.phil.ruu.nl> hjv@phil.ruu.nl (Hendrik Jan Veenstra) writes:
  13. >And what if 'value' lies *between* max1 and max2...?  Suppose you have to
  14. >check for that too...
  15. >So, supposing max1 is the biggest of the two, this would yield something like
  16. >
  17. >         if (value > max1)
  18. >         {
  19. >            max2 = max1;
  20. >            max1 = value;
  21. >         }
  22. >         else if (value > max2)
  23. >         {
  24. >            max2 = value;
  25. >         }
  26.  
  27. A faster way to write this would be:
  28.    if (value > max2)
  29.    {
  30.       if (value > max1)
  31.       {
  32.          max2 = max1;
  33.          max1 = value;
  34.       } else {
  35.          max2 = value;
  36.       }
  37.    }
  38. This way, when the value is smaller than both maximums, only one compare is 
  39. done.
  40. -----
  41. Damien Neil   dpn2@po.cwru.edu    "Until somebody debugs reality, the best
  42. Case Western Reserve University    I can do is a quick patch here and there."
  43. CMPS/EEAP double majoring masochist               - Erik Green
  44.