[trick] Algoritma binary search


<?php
function BinarySearch( $looping, $array, $key, $low, $high )
{
if( $low > $high )
{
return -1;
}

$looping++;


$middle = intval( ( $low+$high )/2 );
echo "
Looping ke-$looping <br>
middle = ( $low+$high )/2 = $middle <br>
";



if ( $array[$middle] == $key )
{
echo "
Jika ".$array[$middle]." == $key, maka $middle <br>
";

return $middle;
}
elseif ( $key < $array[$middle] )
{
echo "
Jika $key < ".$array[$middle].", maka kembali ke fungsi BinarySearch() <br><br>
";

return BinarySearch( $looping, $array, $key, $low, $middle-1 );
}
elseif ( $key > $array[$middle] )
{
echo "
Jika $key > ".$array[$middle].", maka kembali ke fungsi BinarySearch() <br><br>
";

return BinarySearch( $looping, $array, $key, $high, $middle+1 );
}
}
?>

Biasanya digunakan untuk mencari keakuratan untuk pencarian data SQL dan Kecepatan Pencarian data dalam Microsite. contoh penerapannya bisa kita lihat google.

Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: