From: Richard Heathfield on
Juha Nieminen wrote:
> In comp.lang.c++ Richard Heathfield <rjh(a)see.sig.invalid> wrote:
>> Seebs wrote:
>>> On 2010-04-28, Richard Heathfield <rjh(a)see.sig.invalid> wrote:
>> <snip>
>>
>>>> If the data are sorted, bsearch.
>>>> If not, why not?
>>> I do not normally expect something structured like a three-dimensional
>>> array to be sorted, I expect it to be a representation of objects.
>> Then how on earth do you /find/ anything? It's O(N^3), for pity's sake!
>
> Are you saying that if an algorithm is O(N^3) it cannot find anything?

Hyperbole. An O(N^3) algorithm is sometimes necessary, but it is
probably worth spending at least some time trying to come up with a less
costly algorithm.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
From: Richard Heathfield on
James Kanze wrote:
> On May 2, 8:34 am, Richard Heathfield <r...(a)see.sig.invalid> wrote:
>> Seebs wrote:
>
> [...]
>> Why not just write while(i < j && a[i] != 42)?
>
> Because it states the loop invariants up front, in a single
> statement, rather than making the maintenance programmer have to
> figure them out for himself. Maintenance programmers have it
> too easy.

Someone who actually gets it! Thank you.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
From: Richard Heathfield on
Seebs wrote:
> On 2010-05-02, Richard Heathfield <rjh(a)see.sig.invalid> wrote:
<snip>

>
>> This is really an argument about programming philosophy. Juha seems to
>> be in favour of the shortest code that does the job. I'm in favour of
>> code that accurately describes what it's doing. while(i < j) is not an
>> accurate description of the loop if you bomb out as soon as a[i] is 42.
>> Why not just write while(i < j && a[i] != 42)?
>
> Because, in some cases, the checks against a[i] make it harder to follow
> the code.

In my experience, it is generally easier to follow the code if it has a
clear structure. The example posted upthread isn't actually terribly
hard to follow, but I've seen (and had to maintain) some ghastly messes
in my time, where returns and breaks seemed to be inserted into the code
almost at random, and figuring out what the code was supposed to *do*
was way harder than it should have been.

>
> I guess it's a question of how you do the translation.
>
<break example>

<non-break example>

>
> In general, in fact, I prefer the former. The loop is "iterate through
> a". In this case, the idiomatic clarity of "we are looping from 0 to n-1"
> is so much greater that "we are looping from 0 to n-1, and the operation
> is to stop prematurely if..." is much clearer than "we are looping from 0
> to either n-1 or the first value such that....".

That's an interesting take on it, but I would read my version of the
loop control as "look through every element of the array in order until
you've either found what you're looking for or discovered that it's not
there".

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
From: Richard Heathfield on
Leif Roar Moldskred wrote:
> In comp.programming Richard Heathfield <rjh(a)see.sig.invalid> wrote:
>> Then how on earth do you /find/ anything? It's O(N^3), for pity's sake!
>
> No, it isn't. It's O( N ).
>
> (Hint: What's N the count of?)

If N is the number of 3D points, you're right. If N is the size of one
dimension (which is how I was thinking of it), it's O(LMN). If L == M
and L == N, that makes it O(N^3).

For you to be right, however, N would have a granularity greater than 1
for all except the most trivial examples.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
From: Richard Heathfield on
Nick wrote:
> Richard Heathfield <rjh(a)see.sig.invalid> writes:
>
>> Yes, and this question-begging is perfectly in keeping with the
>> requirement that one should never exit from within a loop. (It is,
>> after all, an argument about loops.)
>
> What requirement?

It's a style thing, not a language thing. As with any style thing, it is
only required by its advocates. :-)

Anyway, I was actually making an obscure joke about circular arguments,
which seems to have passed people by. Perhaps some people will catch it
the next time round...

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within