From: "Damir Shayhutdinov" <lost404@gmail.com>
To: "культурный офтопик" <smoke-room@lists.altlinux.org>
Subject: Re: [room] on binary search and merge-sort algos
Date: Mon, 20 Aug 2007 12:31:24 +0400
Message-ID: <679044850708200131w20fca834oad4c5b42de00e281@mail.gmail.com> (raw)
In-Reply-To: <fabhms$ina$1@sea.gmane.org>
> >> А я бы у школьника на зачете такой бинарный бы поиск не принял.
> >> Поскольку он не бинарный, а тернарный - внутри идет ветвление на три
> >> возможных случая.
> > Если уж придираться - то по полной. Там на самом деле четыре возможных
> > случая, включая проверку в while (low <= high).
>
> А вот этого я уже не понимаю. Условие-то в цикле должно быть по-любому,
> от него никуда не денешься.
Смотря что в условие поставить.
>
> Суть бинарного поиска в том, что массив на каждом шаге делится на две
> части, между которыми осуществляется выбор. В том алгоритме, который
> приведён по ссылке массив делится на три части.
Если вы условие выхода из цикла не считаете за "деление массива",
тогда третью часть можно вынести в это условие цикла.
unsigned low = 0;
unsigned mid = 0;
unsigned high = a.length - 1;
while (low <= high && a[mid] != key)
{
mid = (low + high) / 2;
if (a[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
if (a[mid] == key) return mid;
return -(low + 1);
next prev parent reply other threads:[~2007-08-20 8:31 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-08-20 7:18 Michael Shigorin
2007-08-20 7:40 ` Denis Kirienko
2007-08-20 7:53 ` Damir Shayhutdinov
2007-08-20 8:00 ` Denis Kirienko
2007-08-20 8:31 ` Damir Shayhutdinov [this message]
2007-08-20 8:49 ` Denis Kirienko
2007-08-20 14:16 ` Damir Shayhutdinov
2007-08-20 14:38 ` Denis Kirienko
2007-08-20 15:35 ` Denis Kirienko
2007-08-20 17:08 ` Damir Shayhutdinov
2007-08-20 7:48 ` Damir Shayhutdinov
2007-08-20 17:07 ` Alexey Voinov
2007-08-20 19:33 ` Michael Shigorin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=679044850708200131w20fca834oad4c5b42de00e281@mail.gmail.com \
--to=lost404@gmail.com \
--cc=smoke-room@lists.altlinux.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Культурный офтопик
This inbox may be cloned and mirrored by anyone:
git clone --mirror http://lore.altlinux.org/smoke-room/0 smoke-room/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 smoke-room smoke-room/ http://lore.altlinux.org/smoke-room \
smoke-room@lists.altlinux.org smoke-room@lists.altlinux.ru smoke-room@lists.altlinux.com smoke-room@altlinux.ru smoke-room@altlinux.org smoke-room@altlinux.com
public-inbox-index smoke-room
Example config snippet for mirrors.
Newsgroup available over NNTP:
nntp://lore.altlinux.org/org.altlinux.lists.smoke-room
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git