Re: puzzle
- From: spinoza1111@xxxxxxxxx
- Date: 10 Jun 2005 22:45:11 -0700
Arthur J. O'Dwyer wrote:
> On Fri, 10 Jun 2005 spinoza1111@xxxxxxxxx wrote:
> >
> > Darius wrote:
> >> ok here is a puzzle.
> >> there is an integer array whose length is odd, and all the numbers in
> >> the array appear exactly two times except one. Find the number in O(n)
> >> time. Try to do it without using any other data structure.
> >
> [...]
> >
> > Do while arraySize >= 2
> > i = floor(arraySize/2)
> > j = i-1, k = i+1
> > Do while j >= 0 or k < arraySize
> > if j>=0 and a[i]=a[j] { L = j, exit do }
> > if k<arraySize and a[i]=a[k] { L = k, exit do }
> > decrement j, increment k
> > End do
> > If j < 0 and k > arraySize return i
> > delete(i), delete(L) // Changes arraySize
> > End Do
> >
> > As long as DELETE is an atomic and fast operation this algorithm is
> > close to O(n) because the array gets smaller by two entries in the
> > worst case through its major loop, and in the worst case for the inner
> > loop (where the inner loop must search the entire array in both
> > directions) the algorithm is finished by definition.
>
> I think the above paragraph could be cleaned up to yield one of
> those clever "...and thus I am the Pope" arguments, but as it stands,
> it's too obviously confused to be convincing. Nilges' algorithm is
> O(n^2): we're doing O(n) linear searches of a linearly shrinking array.
> And yes, that's assuming DELETE is O(1), which it normally isn't.
The hell with the Pope, and King Billy too. I said "close to" because I
knew that it wasn't O(n) but practical and efficient for reasonable n.
You're doing O(n) searches, times a linearly shrinking array, only in
the worst case. N/2 the time on average it finds U. This doesn't
transform order n^2 because it divides by a constant and not N but it
is none the less a significant reduction.
>
> [snip much dissembling]
>
> The "trick" answer, which has already been posted, is definitely
> the one the OP['s instructor] was thinking of. It's actually quite
> elegant, IMHO, if not terribly useful for anything.
>
"Elegant?" I don't think so. The instructor apparently is sidetracking
the students from discussion of algorithms (which should in no case
impose a barbaric and unstated prejudice for O(n) which is a yahoo's
moral judgement masquerading as science) to the properties of Boolean
operators.
This characterization of a discussion as "dissembling" when it showed
how the theoretical classification has levels within levels explains
the 80% failure rate of enterprise systems, for it clearly shows that
some American "programmers" are invested in cheap tricks and cheap
shots and care not for that form of dialog from which real results can
be obtained.
.
- Follow-Ups:
- Re: puzzle
- From: pete
- Re: puzzle
- From: Christopher Barber
- Re: puzzle
- From: CBFalconer
- Re: puzzle
- References:
- puzzle
- From: Darius
- Re: puzzle
- From: spinoza1111
- Re: puzzle
- From: Arthur J. O'Dwyer
- puzzle
- Prev by Date: Re: Suggestions for double-hashing scheme
- Next by Date: Re: beginner's attempt at hash function
- Previous by thread: Re: puzzle
- Next by thread: Re: puzzle
- Index(es):
Relevant Pages
|