Code

PhoneGap Plugin For Flurry Mobile Analytics

December 29, 2012 @ 2:52 pm · Filed under Code

I’ve been doing a lot of PhoneGap development lately. It’s a convenient way to leverage my Javascript skills in order to produce iOS applications. As part of a recent project, I needed to use Flurry analytics, and I couldn’t find a PhoneGap plugin to do so. So, I wrote one myself.  You can find it on GitHub here: https://github.com/jfpsf/flurry-phonegap-plugin. I hope somebody else finds this useful, and if you have any problems with it, please let me know.

| Comments

PHP Insertion Sort

February 10, 2010 @ 12:38 pm · Filed under Code

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 Code

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

PHP Bubble Sort

December 30, 2007 @ 8:02 pm · Filed under Code

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