From 422694cf2c9b10b9d22f483e49e9a56550fa7ca6 Mon Sep 17 00:00:00 2001 From: DoWake <298666015@qq.com> Date: Sun, 19 May 2024 22:42:06 +0800 Subject: [PATCH] feat: Add PHP Codes for chapter_computational_complexity --- .../iteration.php | 76 ++++++++ .../recursion.php | 80 ++++++++ .../space_complexity.php | 117 +++++++++++ .../time_complexity.php | 184 ++++++++++++++++++ .../worst_best_time_complexity.php | 42 ++++ 5 files changed, 499 insertions(+) create mode 100644 codes/php/chapter_computational_complexity/iteration.php create mode 100644 codes/php/chapter_computational_complexity/recursion.php create mode 100644 codes/php/chapter_computational_complexity/space_complexity.php create mode 100644 codes/php/chapter_computational_complexity/time_complexity.php create mode 100644 codes/php/chapter_computational_complexity/worst_best_time_complexity.php diff --git a/codes/php/chapter_computational_complexity/iteration.php b/codes/php/chapter_computational_complexity/iteration.php new file mode 100644 index 000000000..14c880a65 --- /dev/null +++ b/codes/php/chapter_computational_complexity/iteration.php @@ -0,0 +1,76 @@ + 0; $i--) { + // 通过“入栈操作”模拟“递” + array_push($stack, $i); + } + // 归:返回结果 + while (!empty($stack)) { + // 通过“出栈操作”模拟“归” + $res += array_pop($stack); + } + // res = 1+2+3+...+n + return $res; +} + +/* 尾递归 */ +function tailRecur($n, $res) +{ + // 终止条件 + if ($n === 0) { + return $res; + } + // 尾递归调用 + return tailRecur($n - 1, $res + $n); +} + +/* 斐波那契数列:递归 */ +function fib($n) +{ + // 终止条件 f(1) = 0, f(2) = 1 + if ($n === 1 || $n === 2) { + return $n - 1; + } + // 递归调用 f(n) = f(n-1) + f(n-2) + $res = fib($n - 1) + fib($n - 2); + // 返回结果 f(n) + return $res; +} + +/* Driver Code */ +$n = 5; +$res = 0; + +$res = recur($n); +echo "\n递归函数的求和结果 res = {$res}" . PHP_EOL; + +$res = forLoopRecur($n); +echo "\n使用迭代模拟递归求和结果 res = {$res}" . PHP_EOL; + +$res = tailRecur($n, 0); +echo "\n尾递归函数的求和结果 res = {$res}" . PHP_EOL; + +$res = fib($n); +echo "\n斐波那契数列的第 {$n} 项为 {$res}" . PHP_EOL; diff --git a/codes/php/chapter_computational_complexity/space_complexity.php b/codes/php/chapter_computational_complexity/space_complexity.php new file mode 100644 index 000000000..d35f8ede6 --- /dev/null +++ b/codes/php/chapter_computational_complexity/space_complexity.php @@ -0,0 +1,117 @@ +left = buildTree($n - 1); + $root->right = buildTree($n - 1); + return $root; +} + +/* Driver Code */ +$n = 5; +// 常数阶 +spaceConstant($n); +// 线性阶 +linear($n); +linearRecur($n); +// 平方阶 +quadratic($n); +quadraticRecur($n); +// 指数阶 +$root = buildTree($n); +PrintUtil::printTree($root); diff --git a/codes/php/chapter_computational_complexity/time_complexity.php b/codes/php/chapter_computational_complexity/time_complexity.php new file mode 100644 index 000000000..3ad09025e --- /dev/null +++ b/codes/php/chapter_computational_complexity/time_complexity.php @@ -0,0 +1,184 @@ + 0; $i--) { + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + for ($j = 0; $j < $i; $j++) { + if ($nums[$j] > $nums[$j + 1]) { + // 交换 nums[j] 与 nums[j + 1] + $tmp = $nums[$j]; + $nums[$j] = $nums[$j + 1]; + $nums[$j + 1] = $tmp; + $count += 3; // 元素交换包含 3 个单元操作 + } + } + } + return $count; +} + +/* 指数阶(循环实现) */ +function exponential($n) +{ + $count = 0; + $base = 1; + // 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + for ($i = 0; $i < $n; $i++) { + for ($j = 0; $j < $base; $j++) { + $count++; + } + $base *= 2; + } + // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 + return $count; +} + +/* 指数阶(递归实现) */ +function expRecur($n) +{ + if ($n === 1) { + return 1; + } + return expRecur($n - 1) + expRecur($n - 1) + 1; +} + +/* 对数阶(循环实现) */ +function logarithmic($n) +{ + $count = 0; + while ($n > 1) { + $n = $n / 2; + $count++; + } + return $count; +} + +/* 对数阶(递归实现) */ +function logRecur($n) +{ + if ($n <= 1) { + return 0; + } + return logRecur($n / 2) + 1; +} + +/* 线性对数阶 */ +function linearLogRecur($n) +{ + if ($n <= 1) { + return 1; + } + $count = linearLogRecur($n / 2) + linearLogRecur($n / 2); + for ($i = 0; $i < $n; $i++) { + $count++; + } + return $count; +} + +/* 阶乘阶(递归实现) */ +function factorialRecur($n) +{ + if ($n === 0) { + return 1; + } + $count = 0; + // 从 1 个分裂出 n 个 + for ($i = 0; $i < $n; $i++) { + $count += factorialRecur($n - 1); + } + return $count; +} + +/* Driver Code */ +// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势 +$n = 8; +echo "输入数据大小 n = {$n}" . PHP_EOL; + +$count = timeConstant($n); +echo "常数阶的操作数量 = {$count}" . PHP_EOL; + +$count = linear($n); +echo "线性阶的操作数量 = {$count}" . PHP_EOL; +$count = arrayTraversal(array_fill(0, $n, 0)); +echo "线性阶(遍历数组)的操作数量 = {$count}" . PHP_EOL; + +$count = quadratic($n); +echo "平方阶的操作数量 = {$count}" . PHP_EOL; +$nums = []; +for ($i = 0; $i < $n; $i++) { + $nums[$i] = $n - $i; // [n,n-1,...,2,1] +} +$count = bubbleSort($nums); +echo "平方阶(冒泡排序)的操作数量 = {$count}" . PHP_EOL; + +$count = exponential($n); +echo "指数阶(循环实现)的操作数量 = {$count}" . PHP_EOL; +$count = expRecur($n); +echo "指数阶(递归实现)的操作数量 = {$count}" . PHP_EOL; + +$count = logarithmic($n); +echo "对数阶(循环实现)的操作数量 = {$count}" . PHP_EOL; + +$count = logRecur($n); +echo "对数阶(递归实现)的操作数量 = {$count}" . PHP_EOL; + +$count = linearLogRecur($n); +echo "线性对数阶(递归实现)的操作数量 = {$count}" . PHP_EOL; + +$count = factorialRecur($n); +echo "阶乘阶(递归实现)的操作数量 = {$count}" . PHP_EOL; diff --git a/codes/php/chapter_computational_complexity/worst_best_time_complexity.php b/codes/php/chapter_computational_complexity/worst_best_time_complexity.php new file mode 100644 index 000000000..94079bc8a --- /dev/null +++ b/codes/php/chapter_computational_complexity/worst_best_time_complexity.php @@ -0,0 +1,42 @@ + $value) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) + if ($value === 1) { + return $key; + } + } + return -1; +} + +/* Driver Code */ +for ($i = 0; $i < 10; $i++) { + $n = 100; + $nums = randomNumbers($n); + $index = findOne($nums); + echo "\n数组 [ 1, 2, ..., n ] 被打乱后 = [" . join(', ', $nums) . "]" . PHP_EOL; + echo "数字 1 的索引为 {$index}" . PHP_EOL; +}