home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!linac!att!news.cs.indiana.edu!noose.ecn.purdue.edu!armstrong.ecn.purdue.edu!asean
- From: asean@armstrong.ecn.purdue.edu (Sean Ahern)
- Subject: Re: is it possible to find max & next smallest in one pass?
- Message-ID: <asean.727916943@armstrong.ecn.purdue.edu>
- Sender: news@noose.ecn.purdue.edu (USENET news)
- Organization: Purdue University Engineering Computer Network
- References: <1993Jan24.040702.6193@emr1.emr.ca> <asean.727858796@armstrong.ecn.purdue.edu> <4400@dozo.and.nl>
- Date: 24 Jan 93 23:09:03 GMT
- Lines: 36
-
- 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. Finding three elements,
- >takes O(3*n)= O(n) operations, and so on. But sorting all n elements
- >this way takes up O(n*n) operations, which is, in fact, quite a lousy
- >way of sorting an array.
-
- I went through this same argument with myself as I went to bed and
- realized my mistake.
-
- 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.
-
- --
- Sean Ahern-------------------ahern@purdue.edu-----------------Purdue University
- My name is Batman. You killed my father. Prepare to die.
-