From 93a1e8489d7ba5028aead122a76d864341465913 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Wed, 25 Dec 2024 20:39:43 +0100 Subject: [PATCH 1/6] initial commit --- src/BucketSort.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/BucketSort.php diff --git a/src/BucketSort.php b/src/BucketSort.php new file mode 100644 index 0000000..7565251 --- /dev/null +++ b/src/BucketSort.php @@ -0,0 +1,15 @@ + $arr + * @return array + */ + public static function sort(array $arr): array + { + return $arr; + } +} From 024cc0bea0389d1ea335c01fc167d68e8772fc5d Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Wed, 25 Dec 2024 20:45:39 +0100 Subject: [PATCH 2/6] add split into buckets --- src/BucketSort.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/BucketSort.php b/src/BucketSort.php index 7565251..6a1ab61 100644 --- a/src/BucketSort.php +++ b/src/BucketSort.php @@ -10,6 +10,12 @@ class BucketSort */ public static function sort(array $arr): array { + $length = count($arr); + $bucketSize = (int)sqrt($length); + $buckets = []; + foreach ($arr as $element) { + $buckets[(int)$element%$bucketSize][] = $element; + } return $arr; } } From e211d5f64f6ab5fd7ec1ae3a67ececf685a70dbe Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Thu, 26 Dec 2024 16:09:01 +0100 Subject: [PATCH 3/6] fix distribution into buckets --- src/BucketSort.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/BucketSort.php b/src/BucketSort.php index 6a1ab61..28da57f 100644 --- a/src/BucketSort.php +++ b/src/BucketSort.php @@ -11,10 +11,17 @@ class BucketSort public static function sort(array $arr): array { $length = count($arr); + $min = min($arr); + $max = max($arr); + if ($min == $max) { + return $arr; + } + $range = $max - $min; $bucketSize = (int)sqrt($length); $buckets = []; foreach ($arr as $element) { - $buckets[(int)$element%$bucketSize][] = $element; + $index = (($element - $min) / $range) * ($bucketSize - 1); + $buckets[(int)$index][] = $element; } return $arr; } From 5e98f452045101bc0a5f5b3c86c885014cb7b92a Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Thu, 26 Dec 2024 16:15:14 +0100 Subject: [PATCH 4/6] refactor min and max calculation into condition this should prevent errors on empty arrays --- src/BucketSort.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/BucketSort.php b/src/BucketSort.php index 28da57f..ca69b09 100644 --- a/src/BucketSort.php +++ b/src/BucketSort.php @@ -11,9 +11,7 @@ class BucketSort public static function sort(array $arr): array { $length = count($arr); - $min = min($arr); - $max = max($arr); - if ($min == $max) { + if ($length == 0 || $min= min($arr) == $max = max($arr)) { return $arr; } $range = $max - $min; From a4f0a70603c59c7720b62aa8e0d79c9c8bd1104c Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Thu, 26 Dec 2024 16:16:15 +0100 Subject: [PATCH 5/6] add sorting and concatenating buckets with exchangeable sorting algorithm --- src/BucketSort.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/BucketSort.php b/src/BucketSort.php index ca69b09..e8c9c4e 100644 --- a/src/BucketSort.php +++ b/src/BucketSort.php @@ -4,6 +4,8 @@ class BucketSort { + private static string $sortingAlgorithm = "Src\\Gnomesort"; + /** * @param array $arr * @return array @@ -21,6 +23,13 @@ public static function sort(array $arr): array $index = (($element - $min) / $range) * ($bucketSize - 1); $buckets[(int)$index][] = $element; } - return $arr; + for ($i = 0; $i<$bucketSize; $i++) { + $buckets[$i] = self::$sortingAlgorithm::sort($buckets[$i]); + } + $result = []; + for($i = 0; $i<$bucketSize; $i++) { + $result = array_merge($result, $buckets[$i]); + } + return $result; } } From 042be6a3cbf986d42c7fce1e4b25dd728d5ac63e Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Thu, 26 Dec 2024 16:16:29 +0100 Subject: [PATCH 6/6] add tests for bucket sort --- tests/BucketSortTest.php | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/BucketSortTest.php diff --git a/tests/BucketSortTest.php b/tests/BucketSortTest.php new file mode 100644 index 0000000..eb57bfb --- /dev/null +++ b/tests/BucketSortTest.php @@ -0,0 +1,60 @@ +assertEquals([1,2,3,4,5,6,7,8,9], BucketSort::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() + { + $this->assertEquals([4], BucketSort::sort([4])); + } + + #[Test] + public function test_empty_array() + { + $this->assertEquals([], BucketSort::sort([])); + } + + #[Test] + public function it_should_sort_non_consecutive_numbers_correctly() + { + $this->assertEquals([2,5,6,8,9], BucketSort::sort([5,9,6,2,8])); + } + + #[Test] + public function it_can_sort_array_with_1000_elements() + { + $random = range(1, 1000); + shuffle($random); + $this->assertEquals(range(1, 1000), BucketSort::sort($random)); + } +}