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

Source: Internet
Author: User
This article provides an example of selecting and sorting php matrix transpose Prime number bubble sorting for php beginners. I hope this example will help you. matrix transpose: a matrix refers to a two-dimensional data table in a vertical and horizontal arrangement. Prime number: Prime number formula, also known as Prime... this article provides an example of selecting and sorting php matrix transpose Prime number bubble sorting for php beginners. I hope this example will help you.

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

Regarding Prime number calculation: Prime number formula, also known as prime number formula, indicates a formula that can only generate prime numbers in mathematics. that is to say, this formula can generate all prime numbers one by one, and for each input value, the result of this formula is a prime number, because the number of prime numbers is only a few, therefore, it is generally assumed that the input value is a natural number set (or an integer set and other measurable sets). So far, we have not found a prime number formula that is easy to calculate and meets the preceding conditions, however, we have a lot of knowledge about the nature of the formula.

Bubble-selected sorting: this is not introduced in the two sorting methods.

The sample code is as follows:

 Matrix before transpose: "; foreach ($ matrix as $ line) {echo"
"; Foreach ($ line as $ value) {echo $ value." ";}}$ tm = transposition ($ matrix); echo"
Transposed matrix: "; foreach ($ tm as $ line) {echo"
"; Foreach ($ line as $ element) {echo $ element." ";}}/*** evaluate Prime number * @ param int $ n evaluate 2 ~ $ All prime numbers in n * @ return array returns 2 ~ $ N all prime number sets **/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. "";}/***** bubble sorting ** @ param array $ array of data to be sorted * @ param int $ tag 0 indicates ascending order, 1 indicates the result of sorting from large to small * @ param array **/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." ";}$ data = bubblingsort ($ data); echo"
After sorting from small to large:
"; Foreach ($ data as $ value) {echo $ value." ";}$ data = bubblingsort ($ data, 1); echo"
After sorting from large to small:
"; Foreach ($ data as $ value) {echo $ value. "";}/*** select sorting ** @ param array $ data array * @ param int $ tag 0 indicates sorting from small to large, 1 indicates the result of sorting from large to small * @ param array **/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." ";}$ data = selectsort ($ data); echo"
After sorting from small to large:
"; Foreach ($ data as $ value) {echo $ value." ";}$ data = selectsort ($ data, 1); echo"
After sorting from large to small:
"; Foreach ($ data as $ value) {echo $ value ."";}


Address:

Reprinted at will, but please attach the article address :-)

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.