home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / programm / 3633 < prev    next >
Encoding:
Text File  |  1993-01-28  |  1.5 KB  |  54 lines

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!sun-barr!cs.utexas.edu!torn!utzoo!censor!isgtec!robert
  2. From: robert@isgtec.com (Robert Osborne)
  3. Newsgroups: comp.programming
  4. Subject: Re: is it possible to find max & next smallest in one pass?
  5. Message-ID: <4089@isgtec.isgtec.com>
  6. Date: 26 Jan 93 21:49:41 GMT
  7. References: <51915@seismo.CSS.GOV>
  8. Sender: news@isgtec.com
  9. Lines: 42
  10. X-Newsreader: TIN [version 1.1 PL8]
  11.  
  12. Mike Black (black@seismo.CSS.GOV) wrote:
  13. : jagrant@emr1.emr.ca (John Grant) writes:
  14. : >Given an array of values, is it possible to find the maximum of
  15. : >the array and the next smallest value in a single pass?
  16.  
  17. : Why compare BOTH numbers??  If we find a new max, then the old max is
  18. : obviously the next highest, isn't it??
  19.  
  20. No.  Consider the list 1,3,10,4,5,6 ...
  21.  
  22. But you still only need to compare one of the numbers (usually...)
  23. If the number is less than your current 'next highest' it if
  24. obviously not the max.  If it is larger than the 'next highest'
  25. it may be the new max or it may be the new 'next highest'.
  26.  
  27.     /* assume array[n] != array[m] for m != n */
  28.     double ult;  /* ultimate (ie. max) */
  29.     double penult; /* pen-ultimate (ie. next highest ) */
  30.     extern double array[1000000];
  31.     
  32.     ult = max(array[0],array[1]);
  33.     penult = min(array[0],array[1]);
  34.     for(i=2; i<1000000; i++)
  35.     {
  36.     if( array[i] > penult )
  37.     {
  38.         if( array[i] > ult )
  39.         {
  40.         penult = ult;
  41.         ult = array[i];
  42.         }
  43.         else
  44.         {
  45.         penult = array[i];
  46.         }
  47.     }
  48.     }
  49.  
  50.  
  51. Rob.
  52. --
  53. Robert A. Osborne   ...!uunet.ca!isgtec!robert or robert@isgtec.com
  54.