diogoca/dsalgorithms

此包最新版本(dev-master)没有可用的许可证信息。

数据结构、搜索、排序、数字和字符串算法

dev-master 2020-07-06 23:10 UTC

This package is auto-updated.

Last update: 2024-09-07 08:10:43 UTC


README

使用PHP实现的数据结构、搜索、排序、数字和字符串算法。本项目的目标是研究和实践这些算法。如有任何错误,请通过提交问题告诉我。

安装

建议您使用 Composer 进行安装。

$ composer require diogoca/dsalgorithms

使用

ArrayList

$list = new \DsAlgorithms\Ds\DsList\ArrayList();
$list->add('foo');
$list->add('bar');
$list->add('x');
$list->add('y');
$list->add('z');

echo $list . PHP_EOL; # [foo, bar, x, y, z]
echo $list->get(1) . PHP_EOL; # bar
$list->remove(2);
echo $list . PHP_EOL; # [foo, bar, y, z]

搜索

比较哪些算法调用次数更少。

use DsAlgorithms\Search\BinarySearch;
use DsAlgorithms\Search\SequentialSearch;

$arr = \range(7, 14);
$arrCopy = $arr;

print_r($arr);

echo '14 in index ' . BinarySearch::search($arr, 14) . PHP_EOL;
echo 'binary search calls ' . BinarySearch::$calls . PHP_EOL; 

echo '14 in index ' . SequentialSearch::search($arr, 14) . PHP_EOL; 
echo 'sequencial search calls ' . SequentialSearch::$calls . PHP_EOL;

从bash运行

$ php public/index.php src/String/MaximumOccurringCharacter.php

注意

  • 在 DsAlgorithms\Ds\DsList 命名空间中,我没有使用 List 作为名称,因为那是PHP的保留字。
  • 我使用了 \SplFixedArray,因为在PHP中无法创建固定大小的数组。