[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: What wrong in this code?
- From: Francisco Olarte <folarte@...>
- Date: Mon, 15 May 2017 19:02:18 +0200
On Mon, May 15, 2017 at 2:20 PM, Jorge Eduardo de Araújo Oliveira
<jorgeeduardoaoliveira@gmail.com> wrote:
> I am studying alone... Not homework.
> LOL
Studying should not be laughed at. Even by yourself.
> I am a rookie in Lua programming...
> I want code useful algorithms in Lua just to learn.
> Next is binary search...
Try to do it in the right./fast/elegant way.
Hint: if you have a 4 element vector with indexes 1 to 4 start with
low=0, high=5, not l=1 h=4, and do only one test against the middle
index in the loop. Search for implementation in the internet and
you'll understand why, ask if you do not funderstand.
Nearly all implementation of binary search found in the wild are not
too well coded, even the ones in widely used libraries, it's a
conceptually simple algorithm, but notoriously tricky to get right.
Francisco Olarte.