Archive for PHP

PHP Insertion Sort

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);

| Comments

PHP Sequential or Linear Search Algorithm

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);

| Comments

HipHop for PHP

February 2, 2010 @ 1:52 pm · Filed under PHP, Web 2.0, Web Development

Here’s my quick take on HipHop for PHP that was posted by Larry at ZDNet. I’m attending the HipHop event tonight, and I will add more about it later.

| Comments

OpenDNS Needs Coders

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.

| Comments

SF PHP Meetup

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.

| Comments

PHP Bubble Sort

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 */
}

| Comments

In Defense of PHP

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.

| Comments

© 2008 MobKool Blog | MobKool does not promote or endorse smoking...
My Profile: LinkedIn | Twitter| Facebook