From c10dc8799f96a8dc9d6abbf0bcf3a0cf56dc604d Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Sun, 22 Dec 2024 15:05:35 +0100 Subject: [PATCH 01/20] initial commit --- src/Countingsort.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/Countingsort.php diff --git a/src/Countingsort.php b/src/Countingsort.php new file mode 100644 index 0000000..48c6add --- /dev/null +++ b/src/Countingsort.php @@ -0,0 +1,15 @@ + $arr + * @return array + */ + public static function sort(array $arr): array + { + return $arr; + } +} From 7ba8e40a5d430554da1f24427dcb612ac3d30332 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Sun, 22 Dec 2024 15:08:03 +0100 Subject: [PATCH 02/20] introduce ArrayHelper class --- src/helper/ArrayHelper.php | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/helper/ArrayHelper.php diff --git a/src/helper/ArrayHelper.php b/src/helper/ArrayHelper.php new file mode 100644 index 0000000..963217c --- /dev/null +++ b/src/helper/ArrayHelper.php @@ -0,0 +1,8 @@ + Date: Sun, 22 Dec 2024 15:11:33 +0100 Subject: [PATCH 03/20] add min and max functions using the php build in methods --- src/helper/ArrayHelper.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/helper/ArrayHelper.php b/src/helper/ArrayHelper.php index 963217c..5acafad 100644 --- a/src/helper/ArrayHelper.php +++ b/src/helper/ArrayHelper.php @@ -4,5 +4,25 @@ class ArrayHelper { + /** + * Returns the smallest value of the array + * + * @param array $array + * @return int|float + */ + public static function getMin(array $array): int|float + { + return min($array); + } + /** + * Returns the biggest value of the array + * + * @param array $array + * @return int|float + */ + public static function getMax(array $array): int|float + { + return max($array); + } } From bff64653608c02a62325decdadcd911809025b24 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 11:24:17 +0100 Subject: [PATCH 04/20] add Tests to composer.json autoload --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 43a49db..73b5e73 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ }, "autoload": { "psr-4": { - "Src\\": "src/" + "Src\\": "src/", + "Tests\\": "tests/" } }, "authors": [ From b1dd440f1570a3ab6b85775c0ff6c36c23767ef2 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 11:25:26 +0100 Subject: [PATCH 05/20] add tests for ArrayHelper --- tests/Helper/ArrayHelperTest.php | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/Helper/ArrayHelperTest.php diff --git a/tests/Helper/ArrayHelperTest.php b/tests/Helper/ArrayHelperTest.php new file mode 100644 index 0000000..60fda39 --- /dev/null +++ b/tests/Helper/ArrayHelperTest.php @@ -0,0 +1,66 @@ +assertEquals($expectedMin, ArrayHelper::getMin($array)); + } + + #[Test] + #[DataProvider('minMaxProvider')] + public function it_should_return_the_maximum_value(array $array, int|float $expectedMin, int|float $expectedMax): void + { + $this->assertEquals($expectedMax, ArrayHelper::getMax($array)); + } + + #[Test] + #[DataProvider('intValidationProvider')] + public function it_should_check_if_array_consists_of_only_integers(array $array, bool $expected): void + { + $this->assertEquals($expected, ArrayHelper::consistsOfInt($array)); + } + + /** + * Provides data for min and max tests + * + * @return array> + */ + public static function minMaxProvider(): array + { + return [ + [[1, 2, 3, 4, 5], 1, 5], + [[-10, 0, 10, 20], -10, 20], + [[5.5, 2.2, 3.3, 4.4], 2.2, 5.5], + [[100], 100, 100], + [[-5, -10, -15], -15, -5], + ]; + } + + /** + * Provides data for integer validation test + * + * @return array> + */ + public static function intValidationProvider(): array + { + return [ + [[1, 2, 3, 4, 5], true], + [[1.1, 2.2, 3.3], false], + [[1, 2, '3', 4], false], + [[], true], + [[100, -200, 0], true], + ]; + } +} From 4919883033a01d78ec7a754ec68f01849df52361 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 11:25:51 +0100 Subject: [PATCH 06/20] add consistsOfInt base implementation --- src/helper/ArrayHelper.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/helper/ArrayHelper.php b/src/helper/ArrayHelper.php index 5acafad..fee133a 100644 --- a/src/helper/ArrayHelper.php +++ b/src/helper/ArrayHelper.php @@ -25,4 +25,15 @@ public static function getMax(array $array): int|float { return max($array); } + + /** + * Checks the array for non int values and return false if any occur + * + * @param array $array + * @return bool + */ + public static function consistsOfInt(array $array): bool + { + return count($array) == count(array_filter($array, 'is_int')); + } } From c4de49f550bfa2d1ddcedc730ea46b3fb2041be0 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 13:20:06 +0100 Subject: [PATCH 07/20] add isSorted implementation --- src/helper/ArrayHelper.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/helper/ArrayHelper.php b/src/helper/ArrayHelper.php index fee133a..1d11e89 100644 --- a/src/helper/ArrayHelper.php +++ b/src/helper/ArrayHelper.php @@ -36,4 +36,21 @@ public static function consistsOfInt(array $array): bool { return count($array) == count(array_filter($array, 'is_int')); } + + /** + * Checks the array is sorted + * + * @param array $array + * @return bool + */ + public static function isSorted(array $array): bool + { + $length = count($array); + for ($i = 0; $i < $length - 1; $i++) { + if ($array[$i] > $array[$i + 1]) { + return false; + } + } + return true; + } } From 9305c84031325a2d8afead559ed810d1696d5cf7 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 13:22:31 +0100 Subject: [PATCH 08/20] add tests for isSorted implementation --- tests/Helper/ArrayHelperTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/Helper/ArrayHelperTest.php b/tests/Helper/ArrayHelperTest.php index 60fda39..c6f936f 100644 --- a/tests/Helper/ArrayHelperTest.php +++ b/tests/Helper/ArrayHelperTest.php @@ -32,6 +32,13 @@ public function it_should_check_if_array_consists_of_only_integers(array $array, $this->assertEquals($expected, ArrayHelper::consistsOfInt($array)); } + #[Test] + #[DataProvider('isSortedProvider')] + public function it_should_check_if_array_is_sorted(array $array, bool $expected): void + { + $this->assertEquals($expected, ArrayHelper::isSorted($array)); + } + /** * Provides data for min and max tests * @@ -63,4 +70,22 @@ public static function intValidationProvider(): array [[100, -200, 0], true], ]; } + + /** + * Provides data for isSorted tests + * + * @return array> + */ + public static function isSortedProvider(): array + { + return [ + [[1, 2, 3, 4, 5], true], // Sorted ascending + [[5, 4, 3, 2, 1], false], // Descending + [[1, 1, 1, 1], true], // All equal + [[], true], // Empty array + [[10], true], // Single element + [[1, 2, 2, 3, 4], true], // Contains duplicates, sorted + [[1, 3, 2, 4], false], // Unsorted + ]; + } } From f007f0d3d28785dc6b816398bdaf774a39c00494 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 14:39:10 +0100 Subject: [PATCH 09/20] initialize helper variables --- src/Countingsort.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Countingsort.php b/src/Countingsort.php index 48c6add..8d2e0dc 100644 --- a/src/Countingsort.php +++ b/src/Countingsort.php @@ -2,6 +2,8 @@ namespace Src; +use Src\helper\ArrayHelper; + class Countingsort { /** @@ -10,6 +12,16 @@ class Countingsort */ public static function sort(array $arr): array { + $length = count($arr); + if ($length == 0) { + return $arr; + } + $minElement = ArrayHelper::getMin($arr); + $maxElement = ArrayHelper::getMax($arr); + $helperArray = []; + for($i = $minElement; $i <= $maxElement; $i++){ + $helperArray[$i] = 0; + } return $arr; } } From 910e8938eea7ee27ade77cde57ac9441d39b2b72 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 14:41:07 +0100 Subject: [PATCH 10/20] formatting --- src/Countingsort.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Countingsort.php b/src/Countingsort.php index 8d2e0dc..df556c8 100644 --- a/src/Countingsort.php +++ b/src/Countingsort.php @@ -19,7 +19,7 @@ public static function sort(array $arr): array $minElement = ArrayHelper::getMin($arr); $maxElement = ArrayHelper::getMax($arr); $helperArray = []; - for($i = $minElement; $i <= $maxElement; $i++){ + for ($i = $minElement; $i <= $maxElement; $i++) { $helperArray[$i] = 0; } return $arr; From 255afb6e1efd6ef9ea2696630c58c39741b4ac5d Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 14:42:50 +0100 Subject: [PATCH 11/20] set and sum up helper array --- src/Countingsort.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Countingsort.php b/src/Countingsort.php index df556c8..04d61c6 100644 --- a/src/Countingsort.php +++ b/src/Countingsort.php @@ -22,6 +22,12 @@ public static function sort(array $arr): array for ($i = $minElement; $i <= $maxElement; $i++) { $helperArray[$i] = 0; } + foreach ($arr as $element) { + $helperArray[$element]++; + } + for ($i = $minElement+1; $i <= $maxElement; $i++) { + $helperArray[$i] += $helperArray[$i-1]; + } return $arr; } } From 26a7752e476b768119e536dd1ee3f5deda1c847a Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 14:44:53 +0100 Subject: [PATCH 12/20] extend guard --- src/Countingsort.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Countingsort.php b/src/Countingsort.php index 04d61c6..b97ef1f 100644 --- a/src/Countingsort.php +++ b/src/Countingsort.php @@ -13,7 +13,7 @@ class Countingsort public static function sort(array $arr): array { $length = count($arr); - if ($length == 0) { + if ($length <= 1) { return $arr; } $minElement = ArrayHelper::getMin($arr); From a353236145954d84ca2f19d90f8fbcc5b6915913 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 14:55:29 +0100 Subject: [PATCH 13/20] Rename class --- src/{Countingsort.php => CountingSort.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/{Countingsort.php => CountingSort.php} (97%) diff --git a/src/Countingsort.php b/src/CountingSort.php similarity index 97% rename from src/Countingsort.php rename to src/CountingSort.php index b97ef1f..ec5d6e2 100644 --- a/src/Countingsort.php +++ b/src/CountingSort.php @@ -4,7 +4,7 @@ use Src\helper\ArrayHelper; -class Countingsort +class CountingSort { /** * @param array $arr From 7f76ea9beb77d57f3cf842554b6762d267777c2d Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 14:55:41 +0100 Subject: [PATCH 14/20] formatting --- src/CountingSort.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CountingSort.php b/src/CountingSort.php index ec5d6e2..b762a6e 100644 --- a/src/CountingSort.php +++ b/src/CountingSort.php @@ -25,8 +25,8 @@ public static function sort(array $arr): array foreach ($arr as $element) { $helperArray[$element]++; } - for ($i = $minElement+1; $i <= $maxElement; $i++) { - $helperArray[$i] += $helperArray[$i-1]; + for ($i = $minElement + 1; $i <= $maxElement; $i++) { + $helperArray[$i] += $helperArray[$i - 1]; } return $arr; } From c545be8226de4efb052abcac0b948cdc822e50f8 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 14:56:16 +0100 Subject: [PATCH 15/20] transfer helper to result --- src/CountingSort.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/CountingSort.php b/src/CountingSort.php index b762a6e..f93f9bf 100644 --- a/src/CountingSort.php +++ b/src/CountingSort.php @@ -28,6 +28,11 @@ public static function sort(array $arr): array for ($i = $minElement + 1; $i <= $maxElement; $i++) { $helperArray[$i] += $helperArray[$i - 1]; } - return $arr; + $resultArray = []; + for ($i = 0; $i < $length; $i++) { + $resultArray[$helperArray[$arr[$i]]] = $arr[$i]; + $helperArray[$arr[$i]]--; + } + return $resultArray; } } From fc30d7d504ff822f95f0db8d232365cdf050201d Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 14:56:29 +0100 Subject: [PATCH 16/20] add tests for CountingSort --- tests/CountingSortTest.php | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/CountingSortTest.php diff --git a/tests/CountingSortTest.php b/tests/CountingSortTest.php new file mode 100644 index 0000000..d4c4f10 --- /dev/null +++ b/tests/CountingSortTest.php @@ -0,0 +1,57 @@ + $arr + */ + public function it_should_return_the_sorted_array(array $arr): void + { + $this->assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], CountingSort::sort($arr)); + } + + /** + * @return array> + */ + public static function arrayProvider(): array + { + return [ + [[1, 2, 3, 4, 5, 6, 7, 8, 9]], + [[9, 8, 7, 6, 5, 4, 3, 2, 1]], + [[1, 2, 3, 9, 8, 7, 6, 5, 4]], + [[9, 8, 7, 1, 2, 3, 4, 5, 6]], + [[9, 1, 8, 2, 7, 3, 6, 4, 5]], + [[1, 9, 2, 8, 3, 7, 4, 6, 5]], + [[6, 4, 1, 8, 3, 9, 2, 5, 7]], + ]; + } + + #[Test] + public function it_should_sort_single_element_array(): void + { + $this->assertEquals([4], CountingSort::sort([4])); + } + + #[Test] + public function test_empty_array(): void + { + $this->assertEquals([], CountingSort::sort([])); + } + + #[Test] + public function it_should_sort_non_consecutive_numbers_correctly(): void + { + $this->assertEquals([2, 5, 6, 8, 9], CountingSort::sort([5, 9, 6, 2, 8])); + } +} From 98c7b5876f1f5273b016788e1094c285716fb9df Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 14:58:04 +0100 Subject: [PATCH 17/20] fix index error --- src/CountingSort.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CountingSort.php b/src/CountingSort.php index f93f9bf..67152e8 100644 --- a/src/CountingSort.php +++ b/src/CountingSort.php @@ -30,7 +30,7 @@ public static function sort(array $arr): array } $resultArray = []; for ($i = 0; $i < $length; $i++) { - $resultArray[$helperArray[$arr[$i]]] = $arr[$i]; + $resultArray[$helperArray[$arr[$i]]-1] = $arr[$i]; $helperArray[$arr[$i]]--; } return $resultArray; From 5fccb34b01507586934c8486c68aa42a6ae228f2 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 14:58:39 +0100 Subject: [PATCH 18/20] move decrement and subtraction into array index --- src/CountingSort.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/CountingSort.php b/src/CountingSort.php index 67152e8..b7cad49 100644 --- a/src/CountingSort.php +++ b/src/CountingSort.php @@ -30,8 +30,7 @@ public static function sort(array $arr): array } $resultArray = []; for ($i = 0; $i < $length; $i++) { - $resultArray[$helperArray[$arr[$i]]-1] = $arr[$i]; - $helperArray[$arr[$i]]--; + $resultArray[--$helperArray[$arr[$i]]] = $arr[$i]; } return $resultArray; } From 8eccd982a56cb2d2ed68a94a5c7e7d662d556755 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 15:36:38 +0100 Subject: [PATCH 19/20] Revert "add Tests to composer.json autoload" This reverts commit bff64653608c02a62325decdadcd911809025b24. --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 73b5e73..43a49db 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,7 @@ }, "autoload": { "psr-4": { - "Src\\": "src/", - "Tests\\": "tests/" + "Src\\": "src/" } }, "authors": [ From 8d270b296f63ba7232f8fb6ec2d122f35c488998 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Mon, 23 Dec 2024 15:38:14 +0100 Subject: [PATCH 20/20] add helper testsuite --- phpunit.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phpunit.xml b/phpunit.xml index 309c0de..771a9be 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -12,6 +12,9 @@ tests + + tests/Helper +