note 39994 deleted from function.array-search by felipe
| From: | felipe@php.net | Date: | Sun, 10 Feb 2008 13:11:03 +0000 |
| Subject: | note 39994 deleted from function.array-search by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-135963@lists.php.net to get a copy of this message | ||
Note Submitter: bitmore.co.kr
----
Look Print
<?php
//original
//dennis dot decoene at removthis dot moveit dot be
//18-Jan-2004 12:41
function binsearch( $needle, $haystack ) {
$high = count($haystack);
$low = 0;
while ($high - $low > 1){
$probe = ($high + $low) / 2;
if ($haystack[$probe] < $needle) $low = $probe;
else $high = $probe;
print "high = $high,low = $low,probe = $probe,\$haystack[\$probe] = $haystack[$probe]
<br>";
}
if ($high == count($haystack) || $haystack[$high] != $needle) return false;
else return $high;
}
//orginal
//Look bug
$arr=array(1,3,5,7,9,10,11,13);
$searchfor = 0;
print_r( binsearch($searchfor, $arr) );
//Print Show
//Á¤¼ö1À̳ª 0Àº ´ëÀÔµÈ °ª¿¡ ´ëÇÑ ¹è¿ÀÇ 0À妽º¸¦
ºñ±³Çϴµ¥ ¹®Á¦°¡ ÀÖ´Ù.
// integer 1 or 0 bug Show look show
//high = 4,low = 0,probe = 4,$haystack[$probe] = 9
//high = 2,low = 0,probe = 2,$haystack[$probe] = 5
//high = 1,low = 0,probe = 1,$haystack[$probe] = 3
$searchfor = 1;
print_r( binsearch($searchfor, $arr) );
//Print Show
// integer 1 or 0 bug Show look show
//high = 4,low = 0,probe = 4,$haystack[$probe] = 9
//high = 2,low = 0,probe = 2,$haystack[$probe] = 5
//high = 1,low = 0,probe = 1,$haystack[$probe] = 3
$searchfor = 1;
print_r( binsearch($searchfor, $arr) );
//Print Show
//integer 3
//high = 4,low = 0,probe = 4,$haystack[$probe] = 9
//high = 2,low = 0,probe = 2,$haystack[$probe] = 5
//high = 1,low = 0,probe = 1,$haystack[$probe] = 3
//return 1
?>