1, first to draw a diamond to play, a lot of people learn C in the book are painted, we use PHP painting, draw half.
Idea: How many lines for once, then inside spaces and asterisks for once.
<?php
for ($i =0; $i <=3; $i + +) {
for ($j =0; $j <=3-$i; $j + +) {
Echo ' ';
}
for ($k =0; $k <=2* $i; $k + +) {
Echo ' * ';
}
Echo ' <br/> ';
}
2, bubble sort, C-base algorithm, from small to large to a group of numbers sort.
Idea: This problem from small to large, the first round of the smallest, the second round the second small, third-round row third small, and so on ...
<?php
$arr = Array (3, 2, 1);
$n = count ($arr);
Once per cycle, just run a trip back to the sort of
for ($j =0; $j < $n-1; $j + +) {
The loop finds the largest (smallest), and makes a trip to the back row.
for ($i = $j; $i < $n-1; $i + +) {
if ($arr [$j] > $arr [$i +1]) {
$t = $arr [$j];
$arr [$j] = $arr [$i +1];
$arr [$i +1] = $t;
}
}
}
Print_r ($arr);
3, Yang Hui Triangle, written in PHP.
Idea: Each line of the first and last one is 1, no change, the middle is the front row and the left row of the and, the algorithm is a two-dimensional array to save, another algorithm with a one-dimensional array can also be implemented, a line of output, there is interest to write the play.
1
1 1
1 2 1
1 3 3 1
1 4 6) 4 1
1 5 10 10 5 1
<?php
The first and last of each line is 1, and 6 lines are written.
for ($i =0; $i <6; $i + +) {
$a [$i][0]=1;
$a [$i] [$i]=1;
}
The values of the first and last digits are saved in the array.
for ($i =2; $i <6; $i + +) {
for ($j =1; $j < $i; $j + +) {
$a [$i] [$j] = $a [$i -1][$j -1]+ $a [$i -1][$j];
}
}
Print
for ($i =0; $i <6; $i + +) {
for ($j =0; $j <= $i; $j + +) {
echo $a [$i] [$j]. ' ';
}
Echo ' <br/> ';
}
4, in a group of numbers, required to insert a number, in its original order inserted, maintain the original sorting method.
Idea: Find a location that is larger than the number you want to insert, replace it, and then move the back number back one bit.
<?php
$in = 2;
$arr = Array (1,1,1,3,5,7);
$n = count ($arr);
If the number you want to insert is already the largest, print directly
if ($arr [$n-1] < $in) {
$arr [$n +1] = $in; Print_r ($arr);
}
for ($i =0; $i < $n; $i + +) {
Find the location to insert
if ($arr [$i] >= $in) {
$t 1= $arr [$i];
$arr [$i] = $in;
Move the back of the data back one
for ($j = $i +1; $j < $n +1; $j + +) {
$t 2 = $arr [$j];
$arr [$j] = $t 1;
$t 1 = $t 2;
}
Print
Print_r ($arr);
Die
}
}
5. Sort a set of numbers (fast sorting algorithm).
Idea: The two parts are divided into two sections by a trip, and then they are sorted recursively and finally merged.
<?php
function Q ($array) {
if (count ($array) <= 1) {return $array;}
$key, divided into two sub-arrays
$key = $array [0];
$l = Array ();
$r = Array ();
Recursively sort each other, then synthesize an array
for ($i =1; $i <count ($array); $i + +) {
if ($array [$i] <= $key) {$l [] = $array [$i];}
else {$r [] = $array [$i];}
}
$l = q ($l);
$r = q ($r);
Return Array_merge ($l, Array ($key), $r);
}
$arr = Array (1,2,44,3,4,33);
Print_r (q ($arr));
6. Find the element you want in an array (binary lookup algorithm).
Idea: A value in the array is bounded and then recursively searched until the end.
<?php
function Find ($array, $low, $high, $k) {
if ($low <= $high) {
$mid = Intval (($low + $high)/2);
if ($array [$mid] = = $k) {
return $mid;
}elseif ($k < $array [$mid]) {
Return find ($array, $low, $mid-1, $k);
}else{
Return find ($array, $mid +1, $high, $k);
}
}
Die (' Don't have ... ');
}
Test
$array = Array (2,4,3,5);
$n = count ($array);
$r = Find ($array, 0, $n,
7, merging multiple arrays, without Array_merge (), the topic to the Forum.
Idea: Iterate through each array and re-form a new array.
<?php
function T () {
$c = Func_num_args ()-1;
$a = Func_get_args ();
Print_r ($a);
for ($i =0; $i <= $c; $i + +) {
if (Is_array ($a [$i])) {
for ($j =0; $j <count ($a [$i]); $j + +) {
$r [] = $a [$i] [$j];
}
} else {
Die (' Not a array! ');
}
}
return $r;
}
Test
Print_r (t (range (1,4), Range (1,4), Range (1,4)));
Echo ' <br/> ';
$a = Array_merge (range (1,4), Range (1,4), Range (1,4));
Print_r ($a);
8, the year of the Ox: there is a cow, to 4 years of age can be fertile, one day, the birth are the same cow, to 15-year-old sterilization, no longer able to live, 20-year-old death, asked how many cows in n years. (from the Forum)
<?php
function T ($n) {
static $num = 1
for ($j =1; $j <= $n; $j + +) {
if ($j >=4 && $j <15) {$num ++;t ($n-$j);}
if ($j ==20) {$num--;}
}
return $num;
}
Test
Echo T (8);
Here is a few basic algorithms written in PHP, the importance of the algorithm seems to be not important for PHP programmers, in fact, is very important, classic quote: algorithm + data structure = program. As a real senior PHP programmer, I think it should be familiar to C if you want to be
PHP Basic algorithm