note 105955 added to function.array-search
| From: | DaveC at osu1 dot php dot net | Date: | Tue, 27 Sep 2011 20:18:57 +0000 |
| Subject: | note 105955 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183493@lists.php.net to get a copy of this message | ||
Here is a version of binary search that is done via recursion instead of iteration. Remember that
your data needs to be presorted!
static function Bin_Search(&$needle, &$haystack, $start, $end) {
if($end < $start)
{
return false;
}
$mid = (int)(($end - $start) / 2) + $start;
if($haystack[$mid] > $needle)
{
return Bin_Search($needle, $haystack, $start, $mid - 1);
}
else if($haystack[$mid] < $needle)
{
return Bin_Search($needle, $haystack, $mid + 1, $end);
}
else
{
return true;
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 71.5.32.3
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/105955
Del: integrated -- https://master.php.net/note/delete/105955/integrated
Del: useless -- https://master.php.net/note/delete/105955/useless
Del: bad code -- https://master.php.net/note/delete/105955/bad+code
Del: spam -- https://master.php.net/note/delete/105955/spam
Del: non-english -- https://master.php.net/note/delete/105955/non-english
Del: in docs -- https://master.php.net/note/delete/105955/in+docs
Del: other reasons-- https://master.php.net/note/delete/105955
Reject -- https://master.php.net/note/reject/105955
Search -- https://master.php.net/manage/user-notes.php