Php basic algorithm _ PHP Tutorial

Source: Internet
Author: User
Basic php algorithm. The basic PHP algorithm is a few basic algorithms written in PHP. the importance of algorithms seems to be not very important to PHP programmers. it is actually very important. Classic name: Algorithm + data structure PHP basic algorithm
Here are several basic algorithms written in PHP. the importance of algorithms seems to be less important to PHP programmers.
Actually, it is very important. Classic name: Algorithm + data structure = program. As a real advanced PHP
Programmer, I think you should be familiar with C. If you want to become a real programmer, please study C well and learn how to complete data
Structure and algorithm. Here are just a few basic algorithms, and there are still many things to learn ......
1. First, draw a diamond for fun. many people have painted it in books when learning C. let's draw it in PHP.
Half.
Thinking Path: How many rows for once, and then the space and star number for once in it.
Code snippet
For ($ I = 0; $ I <= 3; $ I ++ ){
For ($ j = 0; $ j <= 3-$ I; $ j ++ ){
Echo '';
}
For ($ k = 0; $ k <= 2 * $ I; $ k ++ ){
Echo '*';
}
Echo'
';
}
2. Bubble sorting: Basic Algorithms in C, sorted by a group of numbers from small to large.
Train of Thought: This question is from small to large, the first round is the smallest, the second round is the second small, the third round is the third small, in turn
And so on ......
Code snippet
$ 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 digits of each row are 1, but they do not change. The middle is the first row in the front row and the last row on the left.
This algorithm is saved using a two-dimensional array, and another algorithm can also be implemented using a one-dimensional array.
A line of output, interested in writing and playing.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Code snippet
// 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]. '';
}
Echo'
';
}
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.
Code snippet
$ 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.
Code snippet
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 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 perform recursive search until the end.
Code snippet
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.
Code snippet
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 $ 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'
';
$ A = array_merge (range (1, 4), range (1, 4 ));
Print_r ($ );
8. OX: there is a cow, which is four years old and can give birth. every year, one of the cows is the same.
15-year-old sterilization, no longer able to live, 20-year-old death, asked how many cows after n years. (From Forum) code snippets
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 less important to PHP programmers. In fact, they are very important. Classic name: Algorithm + data structure...

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.