diff --git a/contents/bogo_sort/bogo_sort.md b/contents/bogo_sort/bogo_sort.md index d23475740..1031e6705 100644 --- a/contents/bogo_sort/bogo_sort.md +++ b/contents/bogo_sort/bogo_sort.md @@ -40,7 +40,7 @@ In code, it looks something like this: {% sample lang="swift" %} [import:13-19, lang:"swift"](code/swift/bogosort.swift) {% sample lang="php" %} -[import:11-16, lang:"php"](code/php/bogo_sort.php) +[import:15-22, lang:"php"](code/php/bogo_sort.php) {% sample lang="nim" %} [import:16-18, lang:"nim"](code/nim/bogo_sort.nim) {% sample lang="ruby" %} diff --git a/contents/bogo_sort/code/php/bogo_sort.php b/contents/bogo_sort/code/php/bogo_sort.php index 96a299b62..41ceb5b99 100644 --- a/contents/bogo_sort/code/php/bogo_sort.php +++ b/contents/bogo_sort/code/php/bogo_sort.php @@ -1,23 +1,31 @@ $array[$i + 1]) { + return false; + } + } - return true; + return true; } function bogo_sort(array $array): array { - if (!is_sorted($array)) shuffle($array); + while (!is_sorted($array)) { + shuffle($array); + } - return $array; + return $array; } $unsorted = [10, 7, 3, 1, 4, 8, 5, 6, 9, 2]; $bogo_sorted = bogo_sort($unsorted); -echo sprintf('Unsorted: %s%s', implode(',', $unsorted), PHP_EOL); -echo sprintf('Sorted: %s%s', implode(',', $bogo_sorted), PHP_EOL); +printf('Unsorted: %s', implode(',', $unsorted)); +echo PHP_EOL; +printf('Sorted: %s', implode(',', $bogo_sorted)); +echo PHP_EOL; diff --git a/contents/bubble_sort/bubble_sort.md b/contents/bubble_sort/bubble_sort.md index 84903ee55..966d46790 100644 --- a/contents/bubble_sort/bubble_sort.md +++ b/contents/bubble_sort/bubble_sort.md @@ -47,7 +47,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with {% sample lang="crystal" %} [import:1-11, lang:"crystal"](code/crystal/bubble.cr) {% sample lang="php" %} -[import:3-15, lang:"php"](code/php/bubble_sort.php) +[import:4-17, lang:"php"](code/php/bubble_sort.php) {% sample lang="lisp" %} [import:3-28, lang:"lisp"](code/lisp/bubble_sort.lisp) {% sample lang="nim" %} diff --git a/contents/bubble_sort/code/php/bubble_sort.php b/contents/bubble_sort/code/php/bubble_sort.php index 50fbf547e..cb7ea77e9 100644 --- a/contents/bubble_sort/code/php/bubble_sort.php +++ b/contents/bubble_sort/code/php/bubble_sort.php @@ -1,21 +1,25 @@ $arr[$j]) { - $tmp = $arr[$j - 1]; - $arr[$j - 1] = $arr[$j]; - $arr[$j] = $tmp; - } + for ($i = 0, $length = count($arr); $i < $length; $i++) { + for ($j = 1; $j < $length; $j++) { + if ($arr[$j - 1] > $arr[$j]) { + $tmp = $arr[$j - 1]; + $arr[$j - 1] = $arr[$j]; + $arr[$j] = $tmp; + } + } } - } - return $arr; + + return $arr; } $unsorted = [1, 2, 6, 47, 4, 9, 3, 7, 8, 23, 15]; $bubble_sorted = bubble_sort($unsorted); -echo sprintf('Unsorted: %s%s', implode(',', $unsorted), PHP_EOL); -echo sprintf('Sorted: %s%s', implode(',', $bubble_sorted), PHP_EOL); +printf('Unsorted: %s', implode(',', $unsorted)); +echo PHP_EOL; +printf('Sorted: %s', implode(',', $bubble_sorted)); +echo PHP_EOL; diff --git a/contents/tree_traversal/code/php/tree_traversal.php b/contents/tree_traversal/code/php/tree_traversal.php index d5fa314a8..fde55046a 100644 --- a/contents/tree_traversal/code/php/tree_traversal.php +++ b/contents/tree_traversal/code/php/tree_traversal.php @@ -1,121 +1,143 @@ id = $id; - $this->children = $children; - } + public function __construct(int $id, array $children = []) + { + $this->id = $id; + $this->children = $children; + } - public function getId(): int { return $this->id; } + public function getId(): int + { + return $this->id; + } - public function getChildren(): array { return $this->children; } + public function getChildren(): array + { + return $this->children; + } - public function addChild(Tree $child): void { $this->children[] = $child; } + public function addChild(Tree $child): void + { + $this->children[] = $child; + } - public function jsonSerialize(): array - { - return [ - 'id' => $this->id, - 'children' => $this->children - ]; - } + public function jsonSerialize(): array + { + return [ + 'id' => $this->id, + 'children' => $this->children, + ]; + } } class TreeTraversal { - public static function dfs_recursive(Tree $tree): void - { - if ($tree->getId()) echo $tree->getId() . PHP_EOL; - foreach ($tree->getChildren() as $child) static::dfs_recursive($child); - } - - public static function dfs_recursive_postorder(Tree $tree): void - { - foreach ($tree->getChildren() as $child) static::dfs_recursive_postorder($child); - echo $tree->getId() . PHP_EOL; - } - - public static function dfs_recursive_inorder_binary(Tree $tree): void - { - switch (count($tree->getChildren())) { - case 2: - static::dfs_recursive_inorder_binary($tree->getChildren()[0]); - echo $tree->getId() . PHP_EOL; - static::dfs_recursive_inorder_binary($tree->getChildren()[1]); - break; - case 1: - static::dfs_recursive_inorder_binary($tree->getChildren()[0]); - echo $tree->getId() . PHP_EOL; - break; - case 0: - echo $tree->getId() . PHP_EOL; - break; - default: - throw new InvalidArgumentException('Not a binary tree!'); - break; + public static function DFSRecursive(Tree $tree): void + { + if ($tree->getId()) { + echo $tree->getId() . PHP_EOL; + } + foreach ($tree->getChildren() as $child) { + static::DFSRecursive($child); + } } - } - public static function dfs_stack(Tree $tree): void - { - $stack = [$tree]; - $temp = null; + public static function DFSRecursivePostorder(Tree $tree): void + { + foreach ($tree->getChildren() as $child) { + static::DFSRecursivePostorder($child); + } + echo $tree->getId() . PHP_EOL; + } - while (null !== ($temp = array_pop($stack))) { - echo $temp->getId() . PHP_EOL; - foreach ($temp->getChildren() as $child) $stack[] = $child; + public static function DFSRecursiveInorderBinary(Tree $tree): void + { + switch (count($tree->getChildren())) { + case 2: + static::DFSRecursiveInorderBinary($tree->getChildren()[0]); + echo $tree->getId() . PHP_EOL; + static::DFSRecursiveInorderBinary($tree->getChildren()[1]); + break; + case 1: + static::DFSRecursiveInorderBinary($tree->getChildren()[0]); + echo $tree->getId() . PHP_EOL; + break; + case 0: + echo $tree->getId() . PHP_EOL; + break; + default: + throw new InvalidArgumentException('Not a binary tree!'); + break; + } } - } - public static function dfs_queue(Tree $tree): void - { - $stack = [$tree]; - $temp = null; + public static function DFSStack(Tree $tree): void + { + $stack = [$tree]; + $temp = null; + + while (null !== ($temp = array_pop($stack))) { + echo $temp->getId() . PHP_EOL; + foreach ($temp->getChildren() as $child) { + $stack[] = $child; + } + } + } - while (null !== ($temp = array_shift($stack))) { - echo $temp->getId() . PHP_EOL; - foreach ($temp->getChildren() as $child) $stack[] = $child; + public static function DFSQueue(Tree $tree): void + { + $stack = [$tree]; + $temp = null; + + while (null !== ($temp = array_shift($stack))) { + echo $temp->getId() . PHP_EOL; + foreach ($temp->getChildren() as $child) { + $stack[] = $child; + } + } } - } } -function generate_tree(int $num_of_rows, int $num_of_children, int $id = -1): Tree +function generate_tree(int $numOfRows, int $numOfChildren, int $id = -1): Tree { - if ($id === -1) $id = 1; - $node = new Tree($id); + if ($id === -1) { + $id = 1; + } + $node = new Tree($id); - if ($num_of_rows > 1) - for ($i = 0; $i < $num_of_children; $i++) { - $child = generate_tree($num_of_rows - 1, $num_of_children, $id * 10 + $i + 1); - $node->addChild($child); + if ($numOfRows > 1) { + for ($i = 0; $i < $numOfChildren; $i++) { + $child = generate_tree($numOfRows - 1, $numOfChildren, $id * 10 + $i + 1); + $node->addChild($child); + } } - return $node; + return $node; } $node = generate_tree(3, 3); -echo sprintf('TreeTraversal in JSON format: %s%s%s', PHP_EOL, json_encode($node), PHP_EOL); echo 'DFS Recursive:' . PHP_EOL; -TreeTraversal::dfs_recursive($node); +TreeTraversal::DFSRecursive($node); echo 'DFS Recursive Postorder:' . PHP_EOL; -TreeTraversal::dfs_recursive_postorder($node); +TreeTraversal::DFSRecursivePostorder($node); echo 'DFS Stack:' . PHP_EOL; -TreeTraversal::dfs_stack($node); +TreeTraversal::DFSStack($node); echo 'DFS Queue:' . PHP_EOL; -TreeTraversal::dfs_queue($node); +TreeTraversal::DFSQueue($node); -// If you want try binary order non-binary tree -// Comment generation of new tree bellow -// If you do that, exception will be thrown +// If you want to try to run binary order on a non-binary tree, +// comment out the generation of the new tree below. +// If you do that, an exception will be thrown $node = generate_tree(3, 2); echo 'DFS Recursive Inorder Binary:' . PHP_EOL; -TreeTraversal::dfs_recursive_inorder_binary($node); \ No newline at end of file +TreeTraversal::DFSRecursiveInorderBinary($node); diff --git a/contents/tree_traversal/tree_traversal.md b/contents/tree_traversal/tree_traversal.md index 2cc090779..f497d1221 100644 --- a/contents/tree_traversal/tree_traversal.md +++ b/contents/tree_traversal/tree_traversal.md @@ -29,7 +29,7 @@ As a note, a `node` struct is not necessary in javascript, so this is an example {% sample lang="swift"%} [import:1-9, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:3-27, lang:"php"](code/php/tree_traversal.php) +[import:4-37, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:1-5, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %} @@ -65,7 +65,7 @@ Because of this, the most straightforward way to traverse the tree might be recu {% sample lang="swift"%} [import:24-30, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:31-35, lang:"php"](code/php/tree_traversal.php) +[import:41-49, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:7-10, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %} @@ -109,7 +109,7 @@ Now, in this case the first element searched through is still the root of the tr {% sample lang="swift"%} [import:32-38, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:37-41, lang:"php"](code/php/tree_traversal.php) +[import:51-57, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:12-15, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %} @@ -148,7 +148,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t {% sample lang="swift"%} [import:40-53, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:43-62, lang:"php"](code/php/tree_traversal.php) +[import:59-78, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:17-31, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %} @@ -197,7 +197,7 @@ In code, it looks like this: {% sample lang="swift"%} [import:55-67, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:64-73, lang:"php"](code/php/tree_traversal.php) +[import:80-91, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:33-41, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %} @@ -238,7 +238,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can {% sample lang="swift"%} [import:69-81, lang:"swift"](code/swift/tree.swift) {% sample lang="php"%} -[import:65-74, lang:"php"](code/php/tree_traversal.php) +[import:93-104, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:43-51, lang:"crystal"](code/crystal/tree-traversal.cr) {% sample lang="go" %}