home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!edcastle!dcs.ed.ac.uk!pdc
- From: pdc@dcs.ed.ac.uk (Paul Crowley)
- Newsgroups: comp.programming
- Subject: Re: is it possible to find max & next smallest in one pass?
- Message-ID: <C1FE09.An2@dcs.ed.ac.uk>
- Date: 25 Jan 93 19:55:20 GMT
- References: <1993Jan24.040702.6193@emr1.emr.ca> <51915@seismo.CSS.GOV> <hjv.727890482@groucho.phil.ruu.nl>
- Sender: cnews@dcs.ed.ac.uk (UseNet News Admin)
- Reply-To: pdc@dcs.ed.ac.uk (Paul Crowley)
- Organization: Edinburgh University
- Lines: 28
-
- If comparisons are more expensive than other operations and you have to
- do this frequently on a big array, there is a standard algorithm that
- only uses (n + ceil(lg n) -1)) comparisons. Supposing, for example,
- that the data objects are Word Tennis players and a comparison is a game
- of Word Tennis; then comparisons are *very* expensive to do compared to
- all other computations. You play people off against each other in
- standard playoffs, like this:
-
- !
- / \
- ! ?
- / \ / \
- ? ! * *
-
- Let ! be the player who won (which is why ! is shown at the top of every
- triangle because ! wins every game ! plays.) ! is the maximum element.
- There are two players who could be in second place, and they are marked
- with a question mark. In general, there are ceil(lg n) of them.
-
- I can't think offhand of an efficient way of implementing this, but I'm
- sure someone can.
-
- For finding max and min simultaneously, there's a simple optimisation:
- compare elements in pairs before comparing to stored max and min
- elements. This takes floor((3n-4)/2) comparisons.
- __ _____
- \/ o\ Paul Crowley pdc@dcs.ed.ac.uk \\ //
- /\__/ Trust me. I know what I'm doing. \X/
-