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

<? Php
Header ("content-type: text/html; charset = utf-8 ");
/**
 *
* Basic PHP Data structure algorithm
* 1. Matrix transpose
* 2. Calculate the prime number
* 3. Bubble sorting
* 4. Select sorting
*//**
* Matrix transpose
 *
* @ Param array $ matrix to be transposed
* @ Param array return transposed matrix
**/
Function transposition ($ matrix ){
$ I = 0;
$ J = 0;
Foreach ($ matrix as $ line ){
Foreach ($ line as $ element ){
$ Tm [$ j ++] [$ I] = $ element;
  }
$ J = 0;
$ I ++;
 }
Return $ tm;
}
$ Matrix = array (
Array (1, 2, 3, 'A '),
Array (4,5, 6, 'B '),
Array (7,8, 9, 'C '),
);
Echo "<br/> transpose matrix :";
Foreach ($ matrix as $ line ){
Echo "<br/> ";
Foreach ($ line as $ value ){
Echo $ value. "& nbsp ;";
 }
}
$ Tm = transposition ($ matrix );
Echo "<br/> transposed matrix :";
Foreach ($ tm as $ line ){
Echo "<br/> ";
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 "<br/> 2 ~ The prime numbers in {$ n} are: <br/> ";
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 "<br/> before Bubble sorting: <br/> ";
Foreach ($ data as $ value ){
Echo $ value. "& nbsp ;";
}
$ Data = bubblingsort ($ data );
Echo "<br/> sorted in ascending order: <br/> ";
Foreach ($ data as $ value ){
Echo $ value. "& nbsp ;";
}
$ Data = bubblingsort ($ data, 1 );
Echo "<br/> sorted from large to small: <br/> ";
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 "<br/> before sorting: <br/> ";
Foreach ($ data as $ value ){
Echo $ value. "& nbsp ;";
}
$ Data = selectsort ($ data );
Echo "<br/> sorted in ascending order: <br/> ";
Foreach ($ data as $ value ){
Echo $ value. "& nbsp ;";
}
$ Data = selectsort ($ data, 1 );
Echo "<br/> sorted from large to small: <br/> ";
Foreach ($ data as $ value ){
Echo $ value. "& nbsp ;";
}
?>

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.