Basic PHP Algorithms

Source: Internet
Author: User

1. First, draw a diamond for fun. Many people have painted it in books when learning C. We have used PHP to draw half of it.
Idea: How many rows for once, and then the space and star number for once in it.
<? PHP
For ($ I = 0; $ I <= 3; $ I ++ ){
For ($ J = 0; $ j <= 3-$ I; $ J ++ ){
Echo '& nbsp ;';
}
For ($ k = 0; $ k <= 2 * $ I; $ K ++ ){
Echo '*';
}
Echo '<br/> ';
}

 

2. Bubble sorting, basic in C Algorithm Sort the number of groups from small to large.
Idea: This question is from small to large, the first round is the smallest, the second round is the second, the third is the third, and so on ......
<? PHP
$ Arr = array (3, 2, 1 );
$ N = count ($ ARR );
// Run the sorting statement after each loop.
For ($ J = 0; $ J <$ n-1; $ J ++ ){
// Sort the largest (smallest) rows that are not sorted in a loop.
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: the first and last bits of each row are 1, with no changes. The middle is the sum of the first bits in the front row and the last bits on the left. This algorithm is saved using a two-dimensional array, in addition, there is an algorithm that can be implemented using a one-dimensional array. The output of a row is interesting to write and 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 rows of each row are both 1 and 6 rows are written.
For ($ I = 0; $ I <6; $ I ++ ){
$ A [$ I] [0] = 1;
$ A [$ I] [$ I] = 1;
}
// The values except the first and last bits 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]. '& nbsp ;';
}
Echo '<br/> ';
}
4. In a set of numbers, insert a number in the original order to maintain the original sorting method.
Idea: Find the location that is larger than the number to be inserted, replace it, and then move the following number one.
<? PHP
$ In = 2;
$ Arr = array (1, 1, 3, 5, 7 );
$ N = count ($ ARR );
// Print the maximum number of inserts.
If ($ arr [$ n-1] <$ in ){
$ Arr [$ n + 1] = $ in; print_r ($ ARR );
}
For ($ I = 0; $ I <$ N; $ I ++ ){
// Locate the position to insert
If ($ arr [$ I] >=$ in ){
$ T1 = $ arr [$ I];
$ Arr [$ I] = $ in;
// Move the following data one after another
For ($ J = $ I + 1; $ j <$ n + 1; $ J ++ ){
$ T2 = $ arr [$ J];
$ Arr [$ J] = $ T1;
$ T1 = $ T2;
}
// Print
Print_r ($ ARR );
Die;
}
}
5. Sort a group of numbers (quick sorting algorithm ).
Idea: sort the two parts by one click, recursively sort the two parts, and merge them.
<? PHP
Function q ($ array ){
If (count ($ array) <= 1) {return $ array ;}
// Defines $ key and divides it into two sub-arrays.
$ Key = $ array [0];
$ L = array ();
$ R = array ();
// Perform recursive sorting respectively and 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, 4, 3, 4, 33 );
Print_r (Q ($ ARR ));

6. Search for the elements you need in an array (Binary Search Algorithm ).
Idea: Use a value in the array as the boundary, and then perform recursive search 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 ('not have ...');
}
// Test
$ Array = array (2, 4, 3, 5 );
$ N = count ($ array );
$ R = find ($ array, 0, $ n,
7. Merge multiple arrays without using array_merge.
Idea: traverse each array and form a new array.
<? PHP
Function T (){
$ C = func_num_args ()-1;
$ A = func_get_args ();
// Print_r ($ );
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 ));
Print_r ($ );
8. Every year, a cow has the same birth as a 4-year-old cow. It is sterilized at the age of 15 and no longer gives birth to a 20-year-old cow, ask how many cows are there in N years. (From 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 are several basic algorithms written in PHP. The importance of algorithms seems to be Program Personnel are not very important. In fact, they are very important. Classic name: algorithm + Data Structure = program. As a real high-level PHP programmer, I think you should be familiar with C. If you want to become

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.