rnix / bubble-sorter
An over-engineered implementation of bubble sort
dev-master
2019-02-12 14:26 UTC
Requires
- php: ^7.2
Requires (Dev)
- phpstan/phpstan: ^0.11.1
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2025-04-13 04:07:52 UTC
README
A library which can sort items using bubble sort. Written with OOP, SOLID and love.
Sorting
$floatsToSort = [5, 8, 3, 12, 3]; $comparer = new \BubbleSorter\Comparer\FloatComparer(); $floatItemsCreator = new \BubbleSorter\Item\Creator\FloatCreator(); $floatItemsFromListCreator = new \BubbleSorter\Item\Creator\FloatsFromListCreator($floatItemsCreator); $items = $floatItemsFromListCreator->createFromList(...$floatsToSort); $collection = new \BubbleSorter\Collection\Collection(...$items); $indexesGeneratorFactory = new \BubbleSorter\Generator\Factory\IndexesGeneratorFactory(); $indexesGeneratorBuilder = new \BubbleSorter\Generator\Builder\Builder($indexesGeneratorFactory); $comparer = new \BubbleSorter\Comparer\FloatComparer(); $swapper = new \BubbleSorter\Swapper\Swapper(); $sorter = new \BubbleSorter\Sorter\Sorter($indexesGeneratorBuilder, $comparer, $swapper); $sortedCollection = $sorter->sort($collection); $itemsGeneratorFactory = new \BubbleSorter\Generator\Factory\ItemsGeneratorFactory(); $itemsGeneratorBuilder = new \BubbleSorter\Generator\Builder\Builder($itemsGeneratorFactory); $extractor = new \BubbleSorter\Item\Extractor\FloatsToArrayExtractor($itemsGeneratorBuilder); $sortedFloats = $extractor->extract($sortedCollection); print_r($sortedFloats);
outputs
Array
(
[0] => 3
[1] => 3
[2] => 5
[3] => 8
[4] => 12
)
See examples in example
directory.
Tests
Work is in progress.
License
The MIT License