note 39994 added to function.array-search

From: Date: Tue, 17 Feb 2004 23:54:54 +0000
Subject: note 39994 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-65326@lists.php.net to get a copy of this message
Look Print //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 ---- Manual Page -- http://www.php.net/manual/en/function.array-search.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39994 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39994&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39994&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65326) next »