PHP Data structure Basic Algorithm 1: Matrix transpose for prime number bubble sort selection for sorting

Source: Internet
Author: User
PHP Data structure Basic Algorithm 1: Matrix transpose for prime number bubble sort selection for sorting
 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." ";}?>

 

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.