Common php Algorithms and time complexity

Source: Internet
Author: User
This article provides a detailed analysis of common php Algorithms and time complexity. For more information, see sort by order of magnitude. Common time complexity: constant order O (1), logarithm order O (log2n), linear order O (n), linear logarithm order O (nlog2n), square order O (n2 ), cubic order O (n3)

The code is as follows:


// Binary search O (log2n)
Function erfen ($ a, $ l, $ h, $ f ){
If ($ l> $ h) {return false ;}
$ M = intval ($ l + $ h)/2 );
If ($ a [$ m] ==$ f ){
Return $ m;
} Elseif ($ f <$ a [$ m]) {
Return erfen ($ a, $ l, $ M-1, $ f );
} Else {
Return erfen ($ a, $ m + 1, $ h, $ f );
}

}
$ A = array (88,100 );
Var_dump (erfen ($ a, 0, 5, 1 ));
// Traverse tree O (log2n)
Function bianli ($ p ){
$ A = array ();
Foreach (glob ($ p. '/*') as $ f ){
If (is_dir ($ f )){
$ A = array_merge ($ a, bianli ($ f ));
} Else {
$ A [] = $ f;
}
}
Return $;
}
// Factorial O (log2n)
Function jc ($ n ){
If ($ n <= 1 ){
Return 1;
} Else {
Return $ n * jc ($ n-1 );
}
}
// Quickly search for O (n * log2 (n ))
Function kuaisu ($ ){
$ C = count ($ );
If ($ c <= 1) {return $ ;}
$ L = $ r = array ();
For ($ I = 1; $ I <$ c; $ I ++ ){
If ($ a [$ I] <$ a [0]) {
$ L [] = $ a [$ I];
} Else {
$ R [] = $ a [$ I];
}
}
$ L = kuaisu ($ l );
$ R = kuaisu ($ r );
Return array_merge ($ l, array ($ a [0]), $ r );
}
// Insert sort O (N * N)
Function charu ($ ){
$ C = count ($ );
For ($ I = 1; $ I <$ c; $ I ++ ){
$ T = $ a [$ I];
For ($ j = $ I; $ j> 0 & $ a [$ j-1]> $ t; $ j --){
$ A [$ j] = $ a [$ j-1];
}
$ A [$ j] = $ t;
}
Return $;
}
// Select sorting O (N * N)
Function xuanze ($ ){
$ C = count ($ );
For ($ I = 0; $ I <$ c; $ I ++ ){
For ($ j = $ I + 1; $ j <$ c; $ j ++ ){
If ($ a [$ I]> $ a [$ j]) {
$ T = $ a [$ j];
$ A [$ j] = $ a [$ I];
$ A [$ I] = $ t;
}
}
}
Return $;
}
// Bubble sort O (N * N)
Function maopao ($ ){
$ C = count ($ );
For ($ I = 0; $ I <$ c; $ I ++ ){
For ($ j = $ C-1; $ j> $ I; $ j --){
If ($ a [$ j] <$ a [$ j-1]) {
$ T = $ a [$ j-1];
$ A [$ j-1] = $ a [$ j];
$ A [$ j] = $ t;
}
}
}
Return $;
}


The code is as follows:


/**
* Arrangement and combination
* Use the binary method to select a combination. for example, if you select 5 or 3, you only need to set 3 to 1, therefore, we can obtain 10 combinations, including 01101, 11100, 00111, and 10011.
*
* @ Param: array to be arranged $ arr
* @ Param minimum number $ min_size
* @ Return: a new array combination that meets the conditions
*/
Function plzh ($ arr, $ size = 5 ){
$ Len = count ($ arr );
$ Max = pow (2, $ len );
$ Min = pow (2, $ size)-1;
$ R_arr = array ();
For ($ I = $ min; $ I <$ max; $ I ++ ){
$ Count = 0;
$ T_arr = array ();
For ($ j = 0; $ j <$ len; $ j ++ ){
$ A = pow (2, $ j );
$ T = $ I & $;
If ($ t = $ ){
$ T_arr [] = $ arr [$ j];
$ Count ++;
}
}
If ($ count = $ size ){
$ R_arr [] = $ t_arr;
}
}
Return $ r_arr;
}

$ Pl = pl (array (1, 2, 3, 4, 5, 6, 7), 5 );
Var_dump ($ pl );

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.