php 常用算法和时间复杂度_php技巧
按数量级递增排列,常见的时间复杂度有:常数阶O(1),对数阶O(log2n),线性阶O(n),线性对数阶O(nlog2n),平方阶O(n2),立方阶O(n3)
//二分查找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 return erfen($a, $l, $m-1, $f);
}else{
return erfen($a, $m+1, $h, $f);
}
}
$a = array(1,12,23,67,88,100);
var_dump(erfen($a,0,5,1));
//遍历树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 $a;
}
//阶乘O(log2n)
function jc($n){
if($n return 1;
}else{
return $n*jc($n-1);
}
}
//快速查找 O(n *log2(n))
function kuaisu($a){
$c = count($a);
if($c $l = $r = array();
for ($i=1;$i if($a[$i] $l[] = $a[$i];
}else{
$r[] = $a[$i];
}
}
$l = kuaisu($l);
$r = kuaisu($r);
return array_merge($l,array($a[0]),$r);
}
//插入排序 O(N*N)
function charu($a){
$c = count($a);
for($i=1;$i $t = $a[$i];
for($j=$i;$j>0 && $a[$j-1]>$t;$j--){
$a[$j] = $a[$j-1];
}
$a[$j] = $t;
}
return $a;
}
//选择排序O(N*N)
function xuanze($a){
$c = count($a);
for($i=0;$i for ($j=$i+1;$j if($a[$i]>$a[$j]){
$t = $a[$j];
$a[$j] = $a[$i];
$a[$i] = $t;
}
}
}
return $a;
}
//冒泡排序 O(N*N)
function maopao($a){
$c = count($a);
for($i=0;$i for ($j=$c-1;$j>$i;$j--){
if($a[$j] $t = $a[$j-1];
$a[$j-1] = $a[$j];
$a[$j] = $t;
}
}
}
return $a;
}
/**
* 排列组合
* 采用二进制方法进行组合的选择,如表示5选3时,只需有3位为1就可以了,所以可得到的组合是 01101 11100 00111 10011 01110等10种组合
*
* @param 需要排列的数组 $arr
* @param 最小个数 $min_size
* @return 满足条件的新数组组合
*/
function plzh($arr,$size=5) {
$len = count($arr);
$max = pow(2,$len);
$min = pow(2,$size)-1;
$r_arr = array();
for ($i=$min; $i $count = 0;
$t_arr = array();
for ($j=0; $j $a = pow(2, $j);
$t = $i&$a;
if($t == $a){
$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);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Time complexity analysis of recursive functions involves: identifying base cases and recursive calls. Calculate the time complexity of the base case and each recursive call. Sum the time complexity of all recursive calls. Consider the relationship between the number of function calls and the size of the problem. For example, the time complexity of the factorial function is O(n) because each recursive call increases the recursion depth by 1, giving a total depth of O(n).

Time complexity is a measure of how long a function takes to execute. Common PHP function time complexity problems include nested loops, large array traversals, and recursive calls. Techniques for optimizing time complexity include: using caching to reduce the number of loops simplifying algorithms using parallel processing

Go is an increasingly popular programming language that is designed to be easy to write, easy to read, and easy to maintain, while also supporting advanced programming concepts. Time complexity and space complexity are important concepts in algorithm and data structure analysis. They measure the execution efficiency and memory size of a program. In this article, we will focus on analyzing the time complexity and space complexity in the Go language. Time Complexity Time complexity refers to the relationship between the execution time of an algorithm and the size of the problem. Time is usually expressed in Big O notation

How to analyze algorithms using time complexity and space complexity in C++ Time complexity and space complexity are measures of how long an algorithm takes to run and the space it requires. In software development, we often need to evaluate the efficiency of algorithms to choose the optimal solution. As a high-performance programming language, C++ provides a rich data structure and algorithm library, as well as powerful computing capabilities and memory management mechanisms. This article will introduce how to use time complexity and space complexity analysis algorithms in C++, and explain how to do it through specific code examples

The time complexity of PHP array shuffle sorting is O(n), and the execution time is proportional to the array size. Practical case: Create an array and use the shuffle function to disrupt the sorting and print the shuffled array.

It is crucial to understand the time complexity trap. Optimization strategies include: 1. Use the correct algorithm; 2. Reduce unnecessary copies; 3. Optimize traversal. Practical examples explore optimization methods for calculating the sum of squares of an array, converting a string to uppercase, and finding elements in an unordered array.

Learn the principle and time complexity analysis of counting sorting algorithm in PHP. Counting sorting is a non-comparative sorting algorithm, which is suitable for situations where the data range is small and known. Its basic idea is to count the number of occurrences of each element and then fill it into the output array in turn to achieve sorting. This article will introduce the principles, steps and time complexity analysis of counting sorting, and provide specific PHP code examples. Principle: The principle of counting sorting is relatively simple. Assume that the array to be sorted is an array, and the element range is [0,k]. I

Given an array of size n and multiple integer values, we need to rotate the array starting from a given index k. We want to rotate the array starting from index k as shown below - Example Input: arr[]={1,2,3,4,5} K1=1 K2=3 K3=6Output: 23451 45123 23451 Algorithm STARTStep1->
