If you have ever studied or taken a class that discusses searching and sorting algorithms and their efficiencies you will be familiar with sequential search, binary search and others. However was bucket search ever mentioned? As a PHP programmer you might be using bucket search not even realizing it. Bucket search is absolutely the fastest most efficient and less complex method available. When using a language PHP that supports hash variables it gets even better and more beneficial.
A normal array uses only integers for the keys.
A[0],A[1],A[2]…
A hash array allows you to use strings as the keys.
A[apple],A[orange],A[banana]…
In traditional methods when you want to search an array for an element to be retrieved you must iterate through the array until you find key and retrieve the value. However bucket searches allow you to instantly retrieve the information based on the key value with no iteration, as the key is a pointer to the location in memory where the value is stored. That is the power of bucket search and I welcome any comments of another method that proves otherwise.
Here is some code with bucket search in action.
$fruit_color = array('apple' => 'red', 'orange' => 'orange', 'banana' => 'yellow'); echo 'An apple is '.$fruit_color[apple].'.';
There are however limitations and problems that can arise depending on the type of strings you utilize as the keys in your array, such as the use of special characters, binary characters, utf-8 characters, string is too long, white space, unknown upper and lower case conditions, and others.
Using base64_encode and a few other methods to normalize your key almost all of these issues can be resolved.
$quotes[myKey("This is Sparta!!!\n")] = 'The Movie 300'; function myKey($k) { return base64_encode(strtolower(preg_replace("/[^A-Za-z0-9]/", '', $k))); }
Recent Comments
Archives
Categories
Meta
Social Networks
Recent Posts
About Charles Hays