# Example of selecting sorting by bubble sorting for conversion of php matrix

Source: Internet
Author: User

Matrix transpose: a matrix refers to a two-dimensional data table in a vertical and horizontal arrangement.

Prime number calculation: Prime number formula, also known as the prime number formula, indicates a formula that can only generate prime numbers in mathematics. That is to say, this formula can generate all the prime numbers without leaking, and the results produced by this formula are the prime numbers for each input value. Since the number of prime numbers is only a few, it is generally assumed that the input value is a natural number set (or an integer set and other measurable sets ). So far, people have not found a formula that is easy to calculate and meets the above conditions, but they have a lot of knowledge about the nature of the formula.

Bubble selection and sorting: this is not introduced in the two sorting methods.

Example

 The code is as follows: Copy code transpose matrix :";Foreach (\$ matrix as \$ line ){Echo "
";Foreach (\$ line as \$ value ){Echo \$ value. "& nbsp ;"; }}\$ Tm = transposition (\$ matrix );Echo "
transposed matrix :";Foreach (\$ tm as \$ line ){Echo "
";Foreach (\$ line as \$ element ){Echo \$ element. "& nbsp ;"; }}/*** Prime number* @ Param int \$ n evaluate 2 ~ All prime numbers in \$ n* @ Return array returns 2 ~ \$ N a set of all prime numbers**/Function primenumber (\$ n ){\$ I = 3;\$ Prime = array (2 );\$ Tag = true;While (\$ I <= \$ n ){Foreach (\$ prime as \$ value ){If (\$ I % \$ value = 0 ){\$ Tag = false;Break;   }\$ Tag = true;  }If (\$ tag ){\$ Prime [] = \$ I;  }\$ I ++; }Return \$ prime;}\$ N = 200;\$ Prime = primenumber (\$ n );Echo "
2 ~ The prime numbers in {\$ n} are:
";Foreach (\$ prime as \$ value ){Echo \$ value. "& nbsp ;";}/*** Bubble sorting ** @ Param array \$ array of data to be sorted* @ Param int \$ tag 0 indicates sorting from small to big, and 1 indicates sorting from large to small* @ Param array the result after sorting**/Function bubblingsort (\$ data, \$ tag = 0 ){\$ Arrlen = count (\$ data );For (\$ I = \$ arrlen-1; \$ I >=0; \$ I --){For (\$ j = 0; \$ j <\$ I; \$ j ++ ){If (\$ data [\$ I]> \$ data [\$ j]) {If (\$ tag = 1 ){\$ M = \$ data [\$ j];\$ Data [\$ j] = \$ data [\$ I];\$ Data [\$ I] = \$ m;    }} Else {If (\$ tag = 0 ){\$ M = \$ data [\$ I];\$ Data [\$ I] = \$ data [\$ j];\$ Data [\$ j] = \$ m;    }   }  } }Return \$ data;}\$ Data = array (, 90 );Echo "
before Bubble sorting:
";Foreach (\$ data as \$ value ){Echo \$ value. "& nbsp ;";}\$ Data = bubblingsort (\$ data );Echo "
sorted in ascending order:
";Foreach (\$ data as \$ value ){Echo \$ value. "& nbsp ;";}\$ Data = bubblingsort (\$ data, 1 );Echo "
sorted from large to small:
";Foreach (\$ data as \$ value ){Echo \$ value. "& nbsp ;";}/*** Select sorting ** @ Param array \$ array of data to be sorted* @ Param int \$ tag 0 indicates sorting from small to big, and 1 indicates sorting from large to small* @ Param array the result after sorting**/Function selectsort (\$ data, \$ tag = 0 ){\$ Arrlen = count (\$ data );For (\$ I = 0; \$ I <\$ arrlen-1; \$ I ++ ){For (\$ j = \$ I + 1; \$ j <\$ arrlen; \$ j ++ ){If (\$ data [\$ I]> \$ data [\$ j]) {If (\$ tag = 0 ){\$ M = \$ data [\$ I];\$ Data [\$ I] = \$ data [\$ j];\$ Data [\$ j] = \$ m;    }} Else {If (\$ tag = 1 ){\$ M = \$ data [\$ I];\$ Data [\$ I] = \$ data [\$ j];\$ Data [\$ j] = \$ m;    }   }  } }Return \$ data;}\$ Data = array (, 90 );Echo "
before sorting:
";Foreach (\$ data as \$ value ){Echo \$ value. "& nbsp ;";}\$ Data = selectsort (\$ data );Echo "
sorted in ascending order:
";Foreach (\$ data as \$ value ){Echo \$ value. "& nbsp ;";}\$ Data = selectsort (\$ data, 1 );Echo "
sorted from large to small:
";Foreach (\$ data as \$ value ){Echo \$ value. "& nbsp ;";}?>
Related Keywords:
Related Article

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.