Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
CombSort | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
7 | |
100.00% |
1 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
sort | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
6 |
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 | * CombSort 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 CombSort 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 | $sorted = false; |
43 | $n = \count($list); |
44 | $gap = $n; |
45 | $shrink = 1.3; |
46 | |
47 | if ($n < 2) { |
48 | return $list; |
49 | } |
50 | |
51 | while (!$sorted) { |
52 | $gap = (int) \floor($gap / $shrink); |
53 | |
54 | if ($gap < 2) { |
55 | $gap = 1; |
56 | $sorted = true; |
57 | } |
58 | |
59 | $i = 0; |
60 | while ($i + $gap < $n) { |
61 | if ($list[$i]->compare($list[$i + $gap], $order)) { |
62 | $old = $list[$i]; |
63 | $list[$i] = $list[$i + 1]; |
64 | $list[$i + 1] = $old; |
65 | |
66 | $sorted = false; |
67 | } |
68 | |
69 | ++$i; |
70 | } |
71 | } |
72 | |
73 | return $list; |
74 | } |
75 | } |