Developers Archive for the 'Developer algorithms' Category

Week days algorithim

Week days algorithim Monday, February 5th, 2007

The following code will retrieve the week days between the two given days

<?
$fldmes = 5;
$flddia = 13;
$fldano = 2002;
$fldmes1 = 7;
$flddia1 = 13;
$fldano1 = 2002;
if($fldmes==$fldmes1) {
if($fldmes==1) {
$mes = “Jan”;
$dias = 31;
}
if($fldmes==2) {
$mes = “Feb”;
$dias = 28;
}
if($fldmes==3) {
$mes = “Mar”;
$dias = 31;
}
if($fldmes==4) {
$mes = “Apr”;
$dias = 30;
}
if($fldmes==5) {
$mes = “May”;
$dias = 31;
}
if($fldmes==6) {
$mes = “Jun”;
$dias = 30;
}
if($fldmes==7) {
$mes = “Jul”;
$dias = 31;
}
if($fldmes==8) {
$mes = “Aug”;
$dias = 31;
}
if($fldmes==9) {
$mes = “Sep”;
$dias = 30;
}
if($fldmes==10) {
$mes = “Oct”;
$dias = 31;
}
if($fldmes==11) {
$mes = “Nov”;
$dias = 30;
}
if($fldmes==12) {
$mes = “Dec”;
$dias = 31;
}
for($flddia; $flddia <= $flddia1; $flddia++) {
/* Pulls the numeric value for the day of the week */
$data = strtotime(”$flddia $mes $fldano”);
$data = unixtojd ($data);
$dow = date($data, “w”);
$dow = jddayofweek($dow);
/* 0 -> Sunday | 1 -> Monday | 2 -> Tuesday | 3 -> Wednesday | 4 -> Thursday | 5 -> Friday | 6 -> Saturday */
if($dow!=0 && $dow!=6) {
print “$flddia $mes $ano<br>”;
}
}
} else {
for($fldmes_act=$fldmes; $fldmes_act<=$fldmes1; $fldmes_act++) {
if($fldmes_act==1) {
$mes = “Jan”;
$dias = 31;
}
if($fldmes_act==2) {
$mes = “Feb”;
$dias = 28;
}
if($fldmes_act==3) {
$mes = “Mar”;
$dias = 31;
}
if($fldmes_act==4) {
$mes = “Apr”;
$dias = 30;
}
if($fldmes_act==5) {
$mes = “May”;
$dias = 31;
}
if($fldmes_act==6) {
$mes = “Jun”;
$dias = 30;
}
if($fldmes_act==7) {
$mes = “Jul”;
$dias = 31;
}
if($fldmes_act==8) {
$mes = “Aug”;
$dias = 31;
}
if($fldmes_act==9) {
$mes = “Sep”;
$dias = 30;
}
if($fldmes_act==10) {
$mes = “Oct”;
$dias = 31;
}
if($fldmes_act==11) {
$mes = “Nov”;
$dias = 30;
}
if($fldmes_act==12) {
$mes = “Dec”;
$dias = 31;
}

if($fldmes_act==$fldmes1) {
for($i=1; $i<=$flddia1; $i++) {
/* Pulls the numeric value for the day of the week */
$data = strtotime(”$i $mes $fldano”);
$data = unixtojd ($data);
$dow = date($data, “w”);
$dow = jddayofweek($dow);
/* 0 -> Sunday | 1 -> Monday | 2 -> Tuesday | 3 -> Wednesday | 4 -> Thursday | 5 -> Friday | 6 -> Saturday */
if($dow!=0 && $dow!=6) {
print “$i $mes $ano<br>”;
}
}
} elseif($fldmes==$fldmes_act) {
for($flddia; $flddia<=$dias; $flddia++) {
/* Pulls the numeric value for the day of the week */
$data = strtotime(”$flddia $mes $fldano”);
$data = unixtojd ($data);
$dow = date($data, “w”);
$dow = jddayofweek($dow);
/* 0 -> Sunday | 1 -> Monday | 2 -> Tuesday | 3 -> Wednesday | 4 -> Thursday | 5 -> Friday | 6 -> Saturday */
if($dow!=0 && $dow!=6) {
print “$flddia $mes $ano<br>”;
}
}
} else {
for($i=1; $i<=$dias; $i++) {
/* Pulls the numeric value for the day of the week */
$data = strtotime(”$i $mes $fldano”);
$data = unixtojd ($data);
$dow = date($data, “w”);
$dow = jddayofweek($dow);
/* 0 -> Sunday | 1 -> Monday | 2 -> Tuesday | 3 -> Wednesday | 4 -> Thursday | 5 -> Friday | 6 -> Saturday */
if($dow!=0 && $dow!=6) {
print “$i $mes $ano<br>”;
}
}
}
}
}
?>

Binary search algorithm

Binary search algorithm Monday, February 5th, 2007

Binary search algorithm function. Takes an array as an argument and the element to be searched for in the array. Returns ‘1′ if found, ‘0′ if not. The code can be modified slightly to return the array index of the found element.

<?php
function BinarySearch($ArrayToSearch/*array to search through*/, $SearchFor/*element to search for*/)
{
sort($ArrayToSearch);//must sort the array

//index
$first=0;
$last=count($ArrayToSearch)-1;
$mid=($first+$last)/2;
$SearchFor=strval(trim($SearchFor));
while
(
($first<=$last)&&
(strval(trim($ArrayToSearch[$mid]))!=$SearchFor)
)
{
if(strcmp(strtolower($SearchFor),strtolower($ArrayToSearch[$mid]))<0)
{$last=$mid-1;}//search the upper half
else
if(strcmp(strtolower($SearchFor),strtolower($ArrayToSearch[$mid]))>0)
{$first=$mid+1;}//search the lower half

$mid=($first+$last)/2;//new mid point
}

if(strval(trim($ArrayToSearch[$mid]))==$SearchFor)
//{return $mid;} –> if your objectiv
// e is to return the index
{return 1;}
else
{return 0;}
//{return -1;} –> if your objective
// is to return the index
}
?>

Quick sort for associative arrays

Quick sort for associative arrays Friday, February 2nd, 2007

Here is a simple code for quick sort of a associative array. Associative array is nothing but a array with numeric indices.
<?
function qsort($a,$f) {
qsort_do(&$a,0,Count($a)-1,$f);
}
function qsort_do($a,$l,$r,$f) {
if ($l < $r) {
qsort_partition(&$a,$l,$r,&$lp,&$rp,$f);
qsort_do(&$a,$l,$lp,$f);
qsort_do(&$a,$rp,$r,$f);
}
}
function qsort_partition($a,$l,$r,$lp,$rp,$f) {
$i = $l+1;
$j = $l+1;
while ($j <= $r) {
if ($f($a[$j],$a[$l])) {
$tmp = $a[$j];
$a[$j] = $a[$i];
$a[$i] = $tmp;
$i++;
}
$j++;
}
$x = $a[$l];
$a[$l] = $a[$i-1];
$a[$i-1] = $x;
$lp = $i - 2;
$rp = $i;
}
?>


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.