Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| BubbleSort | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| sort | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Algorithm\Sort; |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Algorithm\Sort; |
| 16 | |
| 17 | /** |
| 18 | * Bubblesort class. |
| 19 | * |
| 20 | * @package phpOMS\Algorithm\Sort; |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | final class BubbleSort implements SortInterface |
| 26 | { |
| 27 | /** |
| 28 | * Constructor |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | * @codeCoverageIgnore |
| 32 | */ |
| 33 | private function __construct() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * {@inheritdoc} |
| 39 | */ |
| 40 | public static function sort(array $list, int $order = SortOrder::ASC) : array |
| 41 | { |
| 42 | $n = \count($list); |
| 43 | |
| 44 | if ($n < 2) { |
| 45 | return $list; |
| 46 | } |
| 47 | |
| 48 | do { |
| 49 | $newN = 0; |
| 50 | |
| 51 | for ($i = 1; $i < $n; ++$i) { |
| 52 | if ($list[$i - 1]->compare($list[$i], $order)) { |
| 53 | $old = $list[$i - 1]; |
| 54 | $list[$i - 1] = $list[$i]; |
| 55 | $list[$i] = $old; |
| 56 | |
| 57 | $newN = $i; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | $n = $newN; |
| 62 | } while ($n > 1); |
| 63 | |
| 64 | return $list; |
| 65 | } |
| 66 | } |