home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!sun-barr!cs.utexas.edu!torn!utzoo!censor!isgtec!robert
- From: robert@isgtec.com (Robert Osborne)
- Newsgroups: comp.programming
- Subject: Re: is it possible to find max & next smallest in one pass?
- Message-ID: <4089@isgtec.isgtec.com>
- Date: 26 Jan 93 21:49:41 GMT
- References: <51915@seismo.CSS.GOV>
- Sender: news@isgtec.com
- Lines: 42
- X-Newsreader: TIN [version 1.1 PL8]
-
- Mike Black (black@seismo.CSS.GOV) wrote:
- : jagrant@emr1.emr.ca (John Grant) writes:
- : >Given an array of values, is it possible to find the maximum of
- : >the array and the next smallest value in a single pass?
-
- : Why compare BOTH numbers?? If we find a new max, then the old max is
- : obviously the next highest, isn't it??
-
- No. Consider the list 1,3,10,4,5,6 ...
-
- But you still only need to compare one of the numbers (usually...)
- If the number is less than your current 'next highest' it if
- obviously not the max. If it is larger than the 'next highest'
- it may be the new max or it may be the new 'next highest'.
-
- /* assume array[n] != array[m] for m != n */
- double ult; /* ultimate (ie. max) */
- double penult; /* pen-ultimate (ie. next highest ) */
- extern double array[1000000];
-
- ult = max(array[0],array[1]);
- penult = min(array[0],array[1]);
- for(i=2; i<1000000; i++)
- {
- if( array[i] > penult )
- {
- if( array[i] > ult )
- {
- penult = ult;
- ult = array[i];
- }
- else
- {
- penult = array[i];
- }
- }
- }
-
-
- Rob.
- --
- Robert A. Osborne ...!uunet.ca!isgtec!robert or robert@isgtec.com
-