Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions codes/php/chapter_computational_complexity/iteration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

namespace chapter_computational_complexity;

class Iteration
class iteration
{
/* for 循环 */
public static function forLoop($n)
public static function forLoop($n): int
{
$res = 0;
// 循环求和 1, 2, ..., n-1, n
Expand All @@ -21,7 +21,7 @@ public static function forLoop($n)
}

/* while 循环 */
public static function whileLoop($n)
public static function whileLoop($n): int
{
$res = 0;
$i = 1; // 初始化条件变量
Expand All @@ -34,7 +34,7 @@ public static function whileLoop($n)
}

/* while 循环(两次更新) */
public static function whileLoopII($n)
public static function whileLoopII($n): int
{
$res = 0;
$i = 1; // 初始化条件变量
Expand All @@ -49,7 +49,7 @@ public static function whileLoopII($n)
}

/* 双层 for 循环 */
public static function nestedForLoop($n)
public static function nestedForLoop($n): string
{
$res = '';
// 循环 i = 1, 2, ..., n-1, n
Expand All @@ -62,6 +62,7 @@ public static function nestedForLoop($n)
return $res;
}
}

/* Driver Code */
$n = 5;

Expand Down
12 changes: 6 additions & 6 deletions codes/php/chapter_computational_complexity/recursion.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace chapter_computational_complexity;

class Recursion
class recursion
{
// 递归
/* 递归 */
public static function recur(int $n): int
{
// 终止条件
Expand All @@ -22,7 +22,7 @@ public static function recur(int $n): int
return $n + $res;
}

// 使用迭代模拟递归
/* 使用迭代模拟递归 */
public static function forLoopRecur(int $n): int
{
// 使用一个显式的数组来模拟系统调用栈
Expand All @@ -42,7 +42,7 @@ public static function forLoopRecur(int $n): int
return $res;
}

// 尾递归
/* 尾递归 */
public static function tailRecur(int $n, int $res): int
{
// 终止条件
Expand All @@ -53,7 +53,7 @@ public static function tailRecur(int $n, int $res): int
return self::tailRecur($n - 1, $res + $n);
}

// 斐波那契数列:递归
/* 斐波那契数列:递归 */
public static function fib(int $n): int
{
// 终止条件 f(1) = 0, f(2) = 1
Expand All @@ -69,7 +69,7 @@ public static function fib(int $n): int

}

// Driver Code
/* Driver Code */
$n = 5;
$res = 0;
Recursion::recur($n);
Expand Down
32 changes: 15 additions & 17 deletions codes/php/chapter_computational_complexity/space_complexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
*/

namespace chapter_computational_complexity;
class SpaceComplexity
class space_complexity

@krahets krahets Nov 19, 2025

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use CamelCase for class names in PHP. Same as all other class names.

Please carefully check the code and ensure that the code follows the coding standard of PHP.

{
// 函数
/* 函数 */
public static function functionMethod(): int
{
// 执行某些操作
return 0;
}

// 常数阶
/* 常数阶 */
public static function constant(int $n): void
{
// 常量、变量、对象占用 O(1) 空间
Expand All @@ -32,7 +32,7 @@ public static function constant(int $n): void
}
}

// 线性阶
/* 线性阶 */
public static function linear(int $n): void
{
// 长度为 n 的数组占用 O(n) 空间
Expand All @@ -49,7 +49,7 @@ public static function linear(int $n): void
}
}

// 线性阶(递归实现)
/* 线性阶(递归实现)*/
public static function linearRecur(int $n): void
{
echo "递归 n = " . $n . "\n";
Expand All @@ -59,7 +59,7 @@ public static function linearRecur(int $n): void
self::linearRecur($n - 1);
}

// 平方阶
/* 平方阶 */
public static function quadratic(int $n): void
{
// 矩阵占用 O(n^2) 空间
Expand All @@ -78,7 +78,7 @@ public static function quadratic(int $n): void
}
}

// 平方阶(递归实现)
/* 平方阶(递归实现) */
public static function quadraticRecur(int $n): int
{
if ($n <= 0) {
Expand All @@ -90,7 +90,7 @@ public static function quadraticRecur(int $n): int
return self::quadraticRecur($n - 1);
}

// 指数阶(建立满二叉树)
/* 指数阶(建立满二叉树)*/
public static function buildTree(int $n)
{
if ($n == 0) {
Expand All @@ -101,20 +101,18 @@ public static function buildTree(int $n)
$root['right'] = self::buildTree($n - 1);
return $root;
}

// Driver Code
}

// Driver Code
/* Driver Code */
$n = 5;
// 常数阶
SpaceComplexity::constant($n);
space_complexity::constant($n);
// 线性阶
SpaceComplexity::linear($n);
SpaceComplexity::linearRecur($n);
space_complexity::linear($n);
space_complexity::linearRecur($n);
// 平方阶
SpaceComplexity::quadratic($n);
SpaceComplexity::quadraticRecur($n);
space_complexity::quadratic($n);
space_complexity::quadraticRecur($n);
// 指数阶
$root = SpaceComplexity::buildTree($n);
$root = space_complexity::buildTree($n);
echo "指数阶(建立满二叉树)完成\n";
79 changes: 39 additions & 40 deletions codes/php/chapter_computational_complexity/time_complexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace chapter_computational_complexity;

class TimeComplexity
class time_complexity
{
// 常数阶
/* 常数阶 */
public static function constant(int $n): int
{
$count = 0;
Expand All @@ -20,7 +20,7 @@ public static function constant(int $n): int
return $count;
}

// 线性阶
/* 线性阶 */
public static function linear(int $n): int
{
$count = 0;
Expand All @@ -30,7 +30,7 @@ public static function linear(int $n): int
return $count;
}

// 线性阶(遍历数组)
/* 线性阶(遍历数组)*/
public static function arrayTraversal(array $nums): int
{
$count = 0;
Expand All @@ -41,7 +41,7 @@ public static function arrayTraversal(array $nums): int
return $count;
}

// 平方阶
/* 平方阶 */
public static function quadratic(int $n): int
{
$count = 0;
Expand All @@ -54,7 +54,7 @@ public static function quadratic(int $n): int
return $count;
}

// 平方阶(冒泡排序)
/* 平方阶(冒泡排序)*/
public static function bubbleSort(array &$nums): int
{
$count = 0; // 计数器
Expand All @@ -74,7 +74,7 @@ public static function bubbleSort(array &$nums): int
return $count;
}

// 指数阶(循环实现)
/* 指数阶(循环实现)*/
public static function exponential(int $n): int
{
$count = 0;
Expand All @@ -90,7 +90,7 @@ public static function exponential(int $n): int
return $count;
}

// 指数阶(递归实现)
/* 指数阶(递归实现)*/
public static function expRecur(int $n): int
{
if ($n == 1) {
Expand All @@ -99,40 +99,40 @@ public static function expRecur(int $n): int
return self::expRecur($n - 1) + self::expRecur($n - 1) + 1;
}

// 对数阶(循环实现)
/* 对数阶(循环实现)*/
public static function logarithmic(int $n): int
{
$count = 0;
while ($n > 1) {
$n = (int)($n / 2);
$n = (int) ($n / 2);
$count++;
}
return $count;
}

// 对数阶(递归实现)
/* 对数阶(递归实现)*/
public static function logRecur(int $n): int
{
if ($n <= 1) {
return 0;
}
return self::logRecur((int)($n / 2)) + 1;
return self::logRecur((int) ($n / 2)) + 1;
}

// 线性对数阶
/* 线性对数阶 */
public static function linearLogRecur(int $n): int
{
if ($n <= 1) {
return 1;
}
$count = self::linearLogRecur((int)($n / 2)) + self::linearLogRecur((int)($n / 2));
$count = self::linearLogRecur((int) ($n / 2)) + self::linearLogRecur((int) ($n / 2));
for ($i = 0; $i < $n; $i++) {
$count++;
}
return $count;
}

// 阶乘阶(递归实现)
/* 阶乘阶(递归实现)*/
public static function factorialRecur(int $n): int
{
if ($n == 0) {
Expand All @@ -148,40 +148,39 @@ public static function factorialRecur(int $n): int

}

// Driver Code
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
/* Driver Code */
$n = 8;
echo "输入数据大小 n = " . $n . "\n";
echo "输入数据大小 n = ".$n."\n";

$count = TimeComplexity::constant($n);
echo "常数阶的操作数量 = " . $count . "\n";
$count = time_complexity::constant($n);
echo "常数阶的操作数量 = ".$count."\n";

$count = TimeComplexity::linear($n);
echo "线性阶的操作数量 = " . $count . "\n";
$count = TimeComplexity::arrayTraversal(array_fill(0, $n, 0));
echo "线性阶(遍历数组)的操作数量 = " . $count . "\n";
$count = time_complexity::linear($n);
echo "线性阶的操作数量 = ".$count."\n";
$count = time_complexity::arrayTraversal(array_fill(0, $n, 0));
echo "线性阶(遍历数组)的操作数量 = ".$count."\n";

$count = TimeComplexity::quadratic($n);
echo "平方阶的操作数量 = " . $count . "\n";
$count = time_complexity::quadratic($n);
echo "平方阶的操作数量 = ".$count."\n";
$nums = array_fill(0, $n, 0);
for ($i = 0; $i < $n; $i++) {
$nums[$i] = $n - $i; // [n,n-1,...,2,1]
}
$count = TimeComplexity::bubbleSort($nums);
echo "平方阶(冒泡排序)的操作数量 = " . $count . "\n";
$count = time_complexity::bubbleSort($nums);
echo "平方阶(冒泡排序)的操作数量 = ".$count."\n";

$count = TimeComplexity::exponential($n);
echo "指数阶(循环实现)的操作数量 = " . $count . "\n";
$count = TimeComplexity::expRecur($n);
echo "指数阶(递归实现)的操作数量 = " . $count . "\n";
$count = time_complexity::exponential($n);
echo "指数阶(循环实现)的操作数量 = ".$count."\n";
$count = time_complexity::expRecur($n);
echo "指数阶(递归实现)的操作数量 = ".$count."\n";

$count = TimeComplexity::logarithmic($n);
echo "对数阶(循环实现)的操作数量 = " . $count . "\n";
$count = TimeComplexity::logRecur($n);
echo "对数阶(递归实现)的操作数量 = " . $count . "\n";
$count = time_complexity::logarithmic($n);
echo "对数阶(循环实现)的操作数量 = ".$count."\n";
$count = time_complexity::logRecur($n);
echo "对数阶(递归实现)的操作数量 = ".$count."\n";

$count = TimeComplexity::linearLogRecur($n);
echo "线性对数阶(递归实现)的操作数量 = " . $count . "\n";
$count = time_complexity::linearLogRecur($n);
echo "线性对数阶(递归实现)的操作数量 = ".$count."\n";

$count = TimeComplexity::factorialRecur($n);
echo "阶乘阶(递归实现)的操作数量 = " . $count . "\n";
$count = time_complexity::factorialRecur($n);
echo "阶乘阶(递归实现)的操作数量 = ".$count."\n";
Loading