home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!paladin.american.edu!howland.reston.ans.net!sol.ctr.columbia.edu!ira.uka.de!slsvaat!us-es.sel.de!reindorf
- From: reindorf@us-es.sel.de (Charles Reindorf)
- Subject: Re: is it possible to find max & next smallest in one pass?
- Message-ID: <1993Jan25.100031.29146@us-es.sel.de>
- Sender: news@us-es.sel.de
- Organization: SEL-Alcatel Line Transmission Systems Dept. US/ES
- References: <1993Jan24.040702.6193@emr1.emr.ca> <1993Jan25.085026.28282@us-es.sel.de> <1993Jan25.085924.28427@us-es.sel.de>
- Date: Mon, 25 Jan 93 10:00:31 GMT
- Lines: 30
-
- 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?
- |>
- |> --
- |> John A. Grant jagrant@emr1.emr.ca
- |> Airborne Geophysics
- |> Geological Survey of Canada, Ottawa
-
- I don't know what context you with to implement your algorithm here. If you are simply
- interested in getting the highest value out, then it is surely a simple O(n) operations
- performed by a linear search and discussed in detail in the responses seen so far on the
- net.
-
- However, there is a method of storing a list of numbers in an array in memory as what is
- called a "heap" (not like the usual memory heap). Maintaining your data in such a heap,
- you can store new values in O(log n) cycles and *extract* the highest value (at the same
- time re-organising the heap) in O(log n) cycles too. The data is still maintained in a
- linear array on disc or in memory and so there is no fragmentation. I am sure a description
- is available in Knuth, but I got it out of a Prentice&Hall book (I think) called (something
- like) "Data Structures and Program Design". Just a guess, but maybe this structure would be
- useful to you.
-
-
- --- Charles Reindorf
-