home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ukma!usenet.ins.cwru.edu!b63519.STUDENT.CWRU.Edu!dpn2
- From: dpn2@po.CWRU.Edu (Damien P. Neil)
- Newsgroups: comp.programming
- Subject: Re: is it possible to find max & next smallest in one pass?
- Date: Sun, 24 Jan 1993 18:42:05 GMT
- Organization: Case Western Reserve University
- Lines: 32
- Message-ID: <dpn2.234.727900925@po.CWRU.Edu>
- References: <1993Jan24.040702.6193@emr1.emr.ca> <51915@seismo.CSS.GOV> <hjv.727890482@groucho.phil.ruu.nl>
- NNTP-Posting-Host: b63519.student.cwru.edu
-
- In article <hjv.727890482@groucho.phil.ruu.nl> hjv@phil.ruu.nl (Hendrik Jan Veenstra) writes:
- >And what if 'value' lies *between* max1 and max2...? Suppose you have to
- >check for that too...
- >So, supposing max1 is the biggest of the two, this would yield something like
- >
- > if (value > max1)
- > {
- > max2 = max1;
- > max1 = value;
- > }
- > else if (value > max2)
- > {
- > max2 = value;
- > }
-
- A faster way to write this would be:
- if (value > max2)
- {
- if (value > max1)
- {
- max2 = max1;
- max1 = value;
- } else {
- max2 = value;
- }
- }
- This way, when the value is smaller than both maximums, only one compare is
- done.
- -----
- Damien Neil dpn2@po.cwru.edu "Until somebody debugs reality, the best
- Case Western Reserve University I can do is a quick patch here and there."
- CMPS/EEAP double majoring masochist - Erik Green
-