Re: Median algorithm
- From: websnarf@xxxxxxxxx
- Date: 10 Apr 2005 13:20:29 -0700
Marcin wrote:
> How I can find median (middle element) in T(n) = O(n) int the worst
> case?
You can find the kth element using the following method (which will
actually move some elements as a side effect.)
1. Split the list into 5 sequential parts equal in length excepting
for the last part which may be off by up to 4. Then consder the
first element from each of the 5 parts and just directly sort them
(its 5 elements, so this is O(1).) Then do the same for the second
element from each list, and so on. On the steps where there are
only 4 elements just sort the 4 of them.
2. Using recursion, find the exact median entry of the middle part,
and call it m.
3. Now partition the whole list according to the pivot "m", in
quicksort style. It can be proven that this m will end up roughly
in the middle 3 fifths of the list (exercise to the reader.)
4. If m ends up positioned exactly at position k, then you are done
(return m.) If the position is less than k, then recurse on the
subparition after m, subtracting the position from k, otherwise
recurse on the subpartition before m.
This surprising algorithm is O(N) (again, exercise to the reader).
Using different part sizes (i.e., 3 or 7, say) doesn't work! As
promising as this sounds -- if you were to actually implement this as a
partitioning mechanism for quicksort it would easily be outrun by
heapsort, and merge sort in terms of performance. Part of the whole
reason that naive quicksorts do so well in terms of performance is
because pivot choice is typically done in some O(1) manner. There are
apparently ways of improving this algorithm (probably having to do with
being able to search for any element between the k1th and k2th
positions) but I have not thought too hard about it.
--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
.
- Follow-Ups:
- Re: Median algorithm
- From: Richard Harter
- Re: Median algorithm
- References:
- Median algorithm
- From: Marcin
- Median algorithm
- Prev by Date: Re: Macromedia's anti-piracy method
- Next by Date: Help with an algorithm to do the following:
- Previous by thread: Re: Median algorithm
- Next by thread: Re: Median algorithm
- Index(es):
Relevant Pages
|