Re: puzzle
- From: Tim Rentsch <txr@xxxxxxxxxxxxxxxxxxx>
- Date: 21 Jun 2005 13:19:45 -0700
pete <pfiland@xxxxxxxxxxxxxx> writes:
> Richard Harter wrote:
> >
> > On Tue, 21 Jun 2005 13:23:53 GMT, pete <pfiland@xxxxxxxxxxxxxx> wrote:
> >
> > >An simple O(n*log(n)) solution for floating types
> > >would be to heapsort the array
> > >and do a sequential check for equality.
> >
> > And, of course, one can do it in O(n) time and O(1) space without the
> > xor trick. To quote someone (me):
> >
> > "If the array may be rearranged then we can again find the singleton
> > with O(1) space and O(n) time. The general idea is to select a pivot
> > value and then partition the array into elements smaller than the
> > pivot, equal to the pivot, and larger than the pivot. If the pivot is
> > the singleton we are done; otherwise repeat on the partition having an
> > odd number of elements."
>
> I think that algorithm suffers from the same O(n * n)
> worst case as simple quick sort.
Richard's algorithm is making use of finding the median
value of the array, which can be done in O(n) time. If you
can guarantee that the array is split nearly in half each
time, then it's possible to subdivide and zero in on the
unique element in O(n) time; using the median as the pivot
value provides that guarantee.
> It's possible that in a worst case,
> that all of your even sized partitions
> might be two elements in size.
> In that case, the partioning will be O(n),
> and the iterations will be O(n).
At each step there are only two partions - the values larger
than the pivot, and the values smaller than the pivot. We
take care to exclude the pivot pair from the partitions, or
if there is no pair value for the pivot then that's the
unique element. With an odd number of elements to start
with and an even number of elements removed, the two
partitions must have an odd number of elements in one case
and an even number in the other case.
The partition with the odd number of elements has the unique
element value, and can't have more than (K+1)/2 elements if
the array being divided had K elements. (In fact, it can't
have even as many as (K+1)/2 elements, but it certainly
can't have *more* than (K+1)/2 elements.)
So the algorithm really does have a worst case performance
that is
O( n + n/2 + n/4 + ... ) = O( n ).
[And, may I add to Richard: clever algorithm!]
.
- Follow-Ups:
- Re: puzzle
- From: pete
- Re: puzzle
- References:
- puzzle
- From: Darius
- Re: puzzle
- From: spinoza1111
- Re: puzzle
- From: Peter Ammon
- Re: puzzle
- From: Arthur J. O'Dwyer
- Re: puzzle
- From: spinoza1111
- Re: puzzle
- From: CBFalconer
- Re: puzzle
- From: spinoza1111
- Re: puzzle
- From: Christopher Barber
- Re: puzzle
- From: spinoza1111
- Re: puzzle
- From: Christopher Barber
- Re: puzzle
- From: Michael Wojcik
- Re: puzzle
- From: spinoza1111
- Re: puzzle
- From: Chris Dams
- Re: puzzle
- From: spinoza1111
- Re: puzzle
- From: pete
- Re: puzzle
- From: Richard Harter
- Re: puzzle
- From: pete
- puzzle
- Prev by Date: Re: Opinion about Data structure
- Next by Date: Re: puzzle
- Previous by thread: Re: puzzle
- Next by thread: Re: puzzle
- Index(es):
Relevant Pages
|