home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!europa.asd.contel.com!howland.reston.ans.net!spool.mu.edu!hri.com!noc.near.net!ceylon!news.tufts.edu!news.tufts.edu!gowen
- From: gowen@jade.tufts.edu (G. Lee Owen)
- Newsgroups: comp.programming
- Subject: Re: is it possible to find max & next smallest in one pass?
- Message-ID: <GOWEN.93Jan24160711@jade.tufts.edu>
- Date: 24 Jan 93 21:19:09 GMT
- References: <1993Jan24.040702.6193@emr1.emr.ca>
- Sender: news@news.tufts.edu (USENET News System)
- Distribution: na
- Organization: Tufts University - Medford, MA
- Lines: 29
- In-Reply-To: jagrant@emr1.emr.ca's message of 24 Jan 93 04:07:02 GMT
-
-
- > Given an array of values, is it possible to find the maximum of
- > the array and the next smallest value in a single pass? The reason
- Yes. Simple solution would be
-
- float max = array[0];
- float min = array[0];
- for (i=0; i<ARRAYMAX; i++) {
- if (array[i] < min) min = array[i];
- if (array[i] > max) max = array[i];
- }
-
- Example is in C, should be easily translatable to most
- languages.
-
- > If so, I guess the same single pass could also find the min and next
- > highest value too?
- Of course, but not quite as simply. You keep a second
- variable -- say, max2 and min2 -- and when you find a new min, min2
- gets the contents of min before min gets the contents of array[i].
- You also have to allow for the case of equal numbers, and you need
- to find a way to set it to the 2nd min if there is nothing less than
- array[0] (that is, the case where the contents of min are never
- changed). (of course, max as well).
-
- --
- --Greg Owen
- gowen@forte.cs.tufts.edu, gowen@jade.tufts.edu
- "C Spot. C Spot run. C Spot run without a core dump! Run, Spot, Run!"
-