home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!paladin.american.edu!darwin.sura.net!seismo!black
- From: black@seismo.CSS.GOV (Mike Black)
- Newsgroups: comp.programming
- Subject: Re: is it possible to find max & next smallest in one pass?
- Message-ID: <51915@seismo.CSS.GOV>
- Date: 24 Jan 93 14:49:57 GMT
- References: <1993Jan24.040702.6193@emr1.emr.ca>
- Sender: usenet@seismo.CSS.GOV
- Organization: Center for Seismic Studies, Arlington, VA
- Lines: 48
- Nntp-Posting-Host: beno.css.gov
-
- In article <1993Jan24.040702.6193@emr1.emr.ca> 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? The reason
- >I want it in one pass, is that the array is actually a data file
- >of a million float values.
- >
- >If so, I guess the same single pass could also find the min and next
- >highest value too?
- >
-
- Seems to me that life is simpler than everyone makes it :-)
-
- Why compare BOTH numbers?? If we find a new max, then the old max is
- obviously the next highest, isn't it?? The same would follow for however
- many maxes you wish to find. No more than one comparison necessary.
-
-
- /* Finds the maximum and next-maximum of of first 1000 pseudo-random numbers.
- * We'll divide by 1000.0 just to get a floating point number.
- * Add a similar primer and one more if-check and you can find the min
- * and next-min too.
- */
-
- main()
- {
- float value, max1, max2, tmp;
- int i;
- max1 = random() / 1000.0; /* Prime the pump */
- max2 = random() / 1000.0;
- if (max1 > max2) { /* Swap the first two if necessary */
- tmp = max1;
- max1 = max2;
- max2 = tmp;
- }
- for (i = 2; i < 1000; i++) {
- value = random() / 1000.0;
- ivalue(value > max1) { /* New max and demote old one */
- max2 = max1;
- max1 = value;
- }
- }
- printf("%g %g\n", max1, max2);
- }
- --
- -----------------------------------------------------------------------------
- black@beno.CSS.GOV land line: 407-676-2923 | I want a computer
- real home: Melbourne, FL home line: 407-242-8619 | that does it all!
- CSI Inc, Computer Software Innovations, Palm Bay, FL
-