home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!gatech!hubcap!ncrcae!ncrhub2!ncrgw2!psinntp!bnlux1.bnl.gov!sscdaq.phy.bnl.gov!murphy
- From: murphy@sscdaq.phy.bnl.gov (Michael Murphy)
- Subject: Re: is it possible to find max & next smallest in one pass?
- Message-ID: <1993Jan25.224200.7195@bnlux1.bnl.gov>
- Sender: news@bnlux1.bnl.gov (Usenet news)
- Organization: Brookhaven National Laboratory Upton, NY
- Date: Mon, 25 Jan 1993 22:42:00 GMT
- Lines: 52
-
-
- >jos@and.nl (Jos Horsmeier) writes:
-
- >>In article <asean.727858796@armstrong.ecn.purdue.edu> asean@armstrong.ecn.purdue.edu (Sean Ahern) writes:
- >>|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 you were able to do this, the algorithm should be able to extend to the
- >|next smallest, and the next, with little or no trouble.
- >|
- >|But the requirement of the algorithm was to do this in one pass. This would
- >|give us an O(n) sorting algorithm that works on arbitrary data. I don't
- >|believe such an animal exists with only one processor.
- >
- >I beg to differ here. Lets do some big Oh calculations. Given n elements
- >in an array, one can find the maximum value in O(n) operations. One can
- >find the next value in O(n) operations too, so both can be done in
- >O(n)+O(n)= O(2*n) operations. But constants don't count here, so the
- >whole job can be done uisng O(n) operations.
-
- This is true, however the first and second largest (smallest) elements of
- an array using can be computed using only n+lg(n)-1 comparisons. For
- n approximately equal to a million, this is about 1,000,020 comparisons as
- opposed to 2,000,000. (Observe that as n increases the lg(n) term becomes
- negligable).
-
- (This was a homework problem in my algorithms course last year. If nobody
- gets it in a week, I'll post the solution).
-
- asean@armstrong.ecn.purdue.edu writes:
-
- >I suppose a good way to find the k smallest elements of an array would be
- >to do a O(n*logn) sort and then pull off the smallest k elements. Though I
- >must admit that if you only want the first 2 smallest, the O(2*n) algorithm
- >would be better for n>4.
-
- The problem of finding the k-smallest elements of an array is often called
- "Selection." Algorithms exist that can perform Selection in guaranteed
- linear time. However they are more of theoretical importance than of practical
- concern because of restrictive constants inside of the big-O (see Corman,
- Rivest, et al "Introduction to Algorithms" for a description of one such
- algorithm). A practical Selection algorithm is nicely described in
- Sedgewick's "Introduction to Algorithms." The idea is based on the Quicksort
- partitioning method. In the average case, this algorithm runs in linear time,
- but, like Quicksort, can be as bad as O(n^2) if the data is already in sorted
- order.
-
-
- Michael Murphy
-