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 + diff --git a/src/CountingSort.php b/src/CountingSort.php new file mode 100644 index 0000000..b7cad49 --- /dev/null +++ b/src/CountingSort.php @@ -0,0 +1,37 @@ + $arr + * @return array + */ + public static function sort(array $arr): array + { + $length = count($arr); + if ($length <= 1) { + return $arr; + } + $minElement = ArrayHelper::getMin($arr); + $maxElement = ArrayHelper::getMax($arr); + $helperArray = []; + 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]; + } + $resultArray = []; + for ($i = 0; $i < $length; $i++) { + $resultArray[--$helperArray[$arr[$i]]] = $arr[$i]; + } + return $resultArray; + } +} diff --git a/src/helper/ArrayHelper.php b/src/helper/ArrayHelper.php new file mode 100644 index 0000000..1d11e89 --- /dev/null +++ b/src/helper/ArrayHelper.php @@ -0,0 +1,56 @@ + $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); + } + + /** + * 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')); + } + + /** + * 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; + } +} 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])); + } +} diff --git a/tests/Helper/ArrayHelperTest.php b/tests/Helper/ArrayHelperTest.php new file mode 100644 index 0000000..c6f936f --- /dev/null +++ b/tests/Helper/ArrayHelperTest.php @@ -0,0 +1,91 @@ +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)); + } + + #[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 + * + * @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], + ]; + } + + /** + * 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 + ]; + } +}