February 10, 2010 @ 12:38 pm
· Filed under Algorithms, PHP, Web Development
Insertion sorts are good when your data is already partially sorted, or if you have a small collection to sort. That’s about the best I can say for them. Here’s two versions in PHP. The first uses two functions:
function insertion_sort($a)
{
for ($i=1; $i < count($a); $i++)
{
inserter(&$a,$i,$a[$i]);
}
}
function inserter($a,$pos,$a_value)
{
$temp = $a[$pos];
$i = $pos;
while($i >= 0 && $a[$i-1] > $a_value)
{
$a[$i] = $a[$i-1];
$i = $i-1;
}
$a[$i] = $temp;
}
The second uses one function:
function insertion_sort($a)
{
for($j=1; $j < count($a); $j++)
{
$temp = $a[$j];
$i = $j;
while(($i >= 0) && ($a[$i-1] > $temp)){
$a[$i] = $a[$i-1];
$i--;
}
$a[$i] = $temp;
}
}
To test them, use the following code:
$haystack = array(1,5,6,3);
insertion_sort(&$haystack);
print_r ($haystack);
Permalink |
February 8, 2010 @ 2:07 pm
· Filed under Algorithms, PHP, Web Development
Sequential search, or linear search, is the simplest searching algorithm. Indeed, given it’s brute force approach of just traversing a collection from the first to last element, it’s hard to justify calling it an algorithm at all. However, here’s an example in PHP:
function sequential_search($needle,$haystack) {
for($i = 0; $i < count($haystack); $i++) {
if ($needle == $i)
return true;
}
return false;
}
Here's an example of how you would use this algorithm to look for the number 3 inside an array:
$haystack = array(1,2,3,4);
$needle = 3;
$success = sequential_search($needle, $haystack);
Permalink |
February 2, 2010 @ 1:52 pm
· Filed under PHP, Web 2.0, Web Development
Permalink |
February 25, 2008 @ 9:52 am
· Filed under PHP, Web 2.0
Hey, if you are a PHP coder who is looking to work at a hot startup, check out these jobs at OpenDNS. This is a great company that provides a really great service.
Permalink |
January 4, 2008 @ 5:19 pm
· Filed under PHP, Web 2.0, Web Development
I went to another SF PHP Meetup last night. As always, it was highly entertaining, and well worth staying in the city until 10 PM. I love the fact that we host these at CNET now. Big thanks to Tougeron for running these meetings. As you can see from Touge’s account, Terry was in full effect (that’s a good thing). Next time, Mager should come instead of watching VT getting their ass kicked.
Permalink |
December 30, 2007 @ 8:02 pm
· Filed under Algorithms, PHP
One of an occasional series of posts on PHP algorithms. I found this originally at United Scripters, and bookmarked it for some reason.
Code
function bubbleSort($array=array()/*could have been a pointer*/){
$lastIndex=sizeof($array)-1;
while(true){
$newStartIndex=false;
$newLastIndex=0;
for($j=($startIndex)?$startIndex:1; $j<=$lastIndex; $j++){
if( $array[$j-1] > $array[$j] ){/*SWAP:*/
$temp=$array[$j-1];
$array[$j-1]=$array[$j];
$array[$j]=$temp;
if($newStartIndex===false){$newStartIndex=$j-1;};
$newLastIndex=$j-1;
};
}
if($newStartIndex===false){return $array;};
$startIndex=$newStartIndex;
$lastIndex=$newLastIndex;
}
/* keep this comment to reuse freely:
Orwant, Hietaniemi, Macdonald algorithm as Php implemented by:
http://www.unitedscripters.com */
}
Permalink |
February 22, 2006 @ 2:03 am
· Filed under PHP
There is lots of PHP hatred on the web. It doesn’t seem to matter that some of the biggest sites on the web run PHP, including Yahoo and CNET’s own Gamespot: PHP still doesn’t get any respect. Anyway, for all PHP lovers out there, here’s two excellent defenses of PHP: Why PHP Scales – A Cranky, Snarky Answer and A pro-php-rant.
Permalink |