From 64198f0562ad8a0fd78cebe8dadb7c265c10d34b Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 10 Aug 2019 23:21:26 +0200 Subject: [PATCH] start impl. sort algorithms --- Algorithm/Sort/BeadSort.php | 0 Algorithm/Sort/BitonicSort.php | 0 Algorithm/Sort/BubbleSort.php | 49 +++++++++++++ Algorithm/Sort/BucketSort.php | 0 Algorithm/Sort/BurstSort.php | 0 Algorithm/Sort/CocktailShakerSort.php | 63 +++++++++++++++++ Algorithm/Sort/CombSort.php | 58 ++++++++++++++++ Algorithm/Sort/CountingSort.php | 0 Algorithm/Sort/CycleSort.php | 0 Algorithm/Sort/FlashSort.php | 0 Algorithm/Sort/GnomeSort.php | 45 ++++++++++++ Algorithm/Sort/HeapSort.php | 0 Algorithm/Sort/InsertionSort.php | 0 Algorithm/Sort/IntroSort.php | 0 Algorithm/Sort/LibrarySort.php | 0 Algorithm/Sort/MergeSort.php | 0 Algorithm/Sort/OddEvenSort.php | 58 ++++++++++++++++ Algorithm/Sort/PancakeSort.php | 0 Algorithm/Sort/PatienceSort.php | 0 Algorithm/Sort/PigenholeSort.php | 0 Algorithm/Sort/PostmanSort.php | 0 Algorithm/Sort/QuickSort.php | 68 +++++++++++++++++++ Algorithm/Sort/RadixSort.php | 0 Algorithm/Sort/SampleSort.php | 0 Algorithm/Sort/SelectionSort.php | 49 +++++++++++++ Algorithm/Sort/ShellSort.php | 0 Algorithm/Sort/SmoothSort.php | 0 Algorithm/Sort/SortInterface.php | 28 ++++++++ Algorithm/Sort/SortOrder.php | 31 +++++++++ Algorithm/Sort/SortableInterface.php | 28 ++++++++ Algorithm/Sort/SpaghettiSort.php | 0 Algorithm/Sort/StrandSort.php | 0 Algorithm/Sort/TimSort.php | 0 Algorithm/Sort/TopologicalSort.php | 0 Algorithm/Sort/TreeSort.php | 0 tests/Algorithm/Sort/BubbleSortTest.php | 58 ++++++++++++++++ .../Algorithm/Sort/CocktailShakerSortTest.php | 58 ++++++++++++++++ tests/Algorithm/Sort/CombSortTest.php | 58 ++++++++++++++++ tests/Algorithm/Sort/GnomeSortTest.php | 58 ++++++++++++++++ tests/Algorithm/Sort/NumericElement.php | 35 ++++++++++ tests/Algorithm/Sort/OddEvenSortTest.php | 58 ++++++++++++++++ tests/Algorithm/Sort/QuickSortTest.php | 58 ++++++++++++++++ tests/Algorithm/Sort/SelectionSortTest.php | 58 ++++++++++++++++ 43 files changed, 918 insertions(+) create mode 100644 Algorithm/Sort/BeadSort.php create mode 100644 Algorithm/Sort/BitonicSort.php create mode 100644 Algorithm/Sort/BubbleSort.php create mode 100644 Algorithm/Sort/BucketSort.php create mode 100644 Algorithm/Sort/BurstSort.php create mode 100644 Algorithm/Sort/CocktailShakerSort.php create mode 100644 Algorithm/Sort/CombSort.php create mode 100644 Algorithm/Sort/CountingSort.php create mode 100644 Algorithm/Sort/CycleSort.php create mode 100644 Algorithm/Sort/FlashSort.php create mode 100644 Algorithm/Sort/GnomeSort.php create mode 100644 Algorithm/Sort/HeapSort.php create mode 100644 Algorithm/Sort/InsertionSort.php create mode 100644 Algorithm/Sort/IntroSort.php create mode 100644 Algorithm/Sort/LibrarySort.php create mode 100644 Algorithm/Sort/MergeSort.php create mode 100644 Algorithm/Sort/OddEvenSort.php create mode 100644 Algorithm/Sort/PancakeSort.php create mode 100644 Algorithm/Sort/PatienceSort.php create mode 100644 Algorithm/Sort/PigenholeSort.php create mode 100644 Algorithm/Sort/PostmanSort.php create mode 100644 Algorithm/Sort/QuickSort.php create mode 100644 Algorithm/Sort/RadixSort.php create mode 100644 Algorithm/Sort/SampleSort.php create mode 100644 Algorithm/Sort/SelectionSort.php create mode 100644 Algorithm/Sort/ShellSort.php create mode 100644 Algorithm/Sort/SmoothSort.php create mode 100644 Algorithm/Sort/SortInterface.php create mode 100644 Algorithm/Sort/SortOrder.php create mode 100644 Algorithm/Sort/SortableInterface.php create mode 100644 Algorithm/Sort/SpaghettiSort.php create mode 100644 Algorithm/Sort/StrandSort.php create mode 100644 Algorithm/Sort/TimSort.php create mode 100644 Algorithm/Sort/TopologicalSort.php create mode 100644 Algorithm/Sort/TreeSort.php create mode 100644 tests/Algorithm/Sort/BubbleSortTest.php create mode 100644 tests/Algorithm/Sort/CocktailShakerSortTest.php create mode 100644 tests/Algorithm/Sort/CombSortTest.php create mode 100644 tests/Algorithm/Sort/GnomeSortTest.php create mode 100644 tests/Algorithm/Sort/NumericElement.php create mode 100644 tests/Algorithm/Sort/OddEvenSortTest.php create mode 100644 tests/Algorithm/Sort/QuickSortTest.php create mode 100644 tests/Algorithm/Sort/SelectionSortTest.php diff --git a/Algorithm/Sort/BeadSort.php b/Algorithm/Sort/BeadSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/BitonicSort.php b/Algorithm/Sort/BitonicSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/BubbleSort.php b/Algorithm/Sort/BubbleSort.php new file mode 100644 index 000000000..3d4a74138 --- /dev/null +++ b/Algorithm/Sort/BubbleSort.php @@ -0,0 +1,49 @@ +compare($list[$i], $order)) { + $old = $list[$i - 1]; + $list[$i - 1] = $list[$i]; + $list[$i] = $old; + + $newN = $i; + } + } + + $n = $newN; + } while ($n > 1); + + return $list; + } +} diff --git a/Algorithm/Sort/BucketSort.php b/Algorithm/Sort/BucketSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/BurstSort.php b/Algorithm/Sort/BurstSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/CocktailShakerSort.php b/Algorithm/Sort/CocktailShakerSort.php new file mode 100644 index 000000000..a9bc43a84 --- /dev/null +++ b/Algorithm/Sort/CocktailShakerSort.php @@ -0,0 +1,63 @@ +compare($list[$i + 1], $order)) { + $old = $list[$i]; + $list[$i] = $list[$i + 1]; + $list[$i + 1] = $old; + + $newEnd = $i; + } + } + + $end = $newEnd - 1; + + for ($i = $end; $i >= $start; --$i) { + if ($list[$i]->compare($list[$i + 1], $order)) { + $old = $list[$i]; + $list[$i] = $list[$i + 1]; + $list[$i + 1] = $old; + + $newStart = $i; + } + } + + $start = $newStart + 1; + } + + return $list; + } +} diff --git a/Algorithm/Sort/CombSort.php b/Algorithm/Sort/CombSort.php new file mode 100644 index 000000000..a48ecd8f3 --- /dev/null +++ b/Algorithm/Sort/CombSort.php @@ -0,0 +1,58 @@ +compare($list[$i + $gap], $order)) { + $old = $list[$i]; + $list[$i] = $list[$i + 1]; + $list[$i + 1] = $old; + + $sorted = false; + } + + ++$i; + } + } + + return $list; + } +} diff --git a/Algorithm/Sort/CountingSort.php b/Algorithm/Sort/CountingSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/CycleSort.php b/Algorithm/Sort/CycleSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/FlashSort.php b/Algorithm/Sort/FlashSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/GnomeSort.php b/Algorithm/Sort/GnomeSort.php new file mode 100644 index 000000000..ff99b086f --- /dev/null +++ b/Algorithm/Sort/GnomeSort.php @@ -0,0 +1,45 @@ + 0 && $list[$j - 1]->compare($list[$j], $order)) { + $old = $list[$j - 1]; + $list[$j - 1] = $list[$j]; + $list[$j] = $old; + + --$j; + } + } + + return $list; + } +} diff --git a/Algorithm/Sort/HeapSort.php b/Algorithm/Sort/HeapSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/InsertionSort.php b/Algorithm/Sort/InsertionSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/IntroSort.php b/Algorithm/Sort/IntroSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/LibrarySort.php b/Algorithm/Sort/LibrarySort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/MergeSort.php b/Algorithm/Sort/MergeSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/OddEvenSort.php b/Algorithm/Sort/OddEvenSort.php new file mode 100644 index 000000000..05089e6ee --- /dev/null +++ b/Algorithm/Sort/OddEvenSort.php @@ -0,0 +1,58 @@ +compare($list[$i + 1], $order)) { + $old = $list[$i]; + $list[$i] = $list[$i + 1]; + $list[$i + 1] = $old; + + $sorted = false; + } + } + + for ($i = 0; $i < $n - 1; $i += 2) { + if ($list[$i]->compare($list[$i + 1], $order)) { + $old = $list[$i]; + $list[$i] = $list[$i + 1]; + $list[$i + 1] = $old; + + $sorted = false; + } + } + } + + return $list; + } +} diff --git a/Algorithm/Sort/PancakeSort.php b/Algorithm/Sort/PancakeSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/PatienceSort.php b/Algorithm/Sort/PatienceSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/PigenholeSort.php b/Algorithm/Sort/PigenholeSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/PostmanSort.php b/Algorithm/Sort/PostmanSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/QuickSort.php b/Algorithm/Sort/QuickSort.php new file mode 100644 index 000000000..cf392bbee --- /dev/null +++ b/Algorithm/Sort/QuickSort.php @@ -0,0 +1,68 @@ +compare($pivot, $order)) { + ++$lo; + } + + while ($list[$hi]->compare($pivot, $order)) { + --$hi; + } + + if ($lo >= $hi) { + return $hi; + } + + $old = $list[$lo]; + $list[$lo] = $list[$hi]; + $list[$hi] = $old; + + ++$lo; + --$hi; + } + } +} diff --git a/Algorithm/Sort/RadixSort.php b/Algorithm/Sort/RadixSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/SampleSort.php b/Algorithm/Sort/SampleSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/SelectionSort.php b/Algorithm/Sort/SelectionSort.php new file mode 100644 index 000000000..daeca283e --- /dev/null +++ b/Algorithm/Sort/SelectionSort.php @@ -0,0 +1,49 @@ +compare($list[$min], $order)) { + $min = $j; + } + } + + if ($min !== $i) { + $old = $list[$i]; + $list[$i] = $list[$min]; + $list[$min] = $old; + } + } + + return $list; + } +} diff --git a/Algorithm/Sort/ShellSort.php b/Algorithm/Sort/ShellSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/SmoothSort.php b/Algorithm/Sort/SmoothSort.php new file mode 100644 index 000000000..e69de29bb diff --git a/Algorithm/Sort/SortInterface.php b/Algorithm/Sort/SortInterface.php new file mode 100644 index 000000000..ff05092f4 --- /dev/null +++ b/Algorithm/Sort/SortInterface.php @@ -0,0 +1,28 @@ +list = [ + new NumericElement(5), + new NumericElement(1), + new NumericElement(4), + new NumericElement(2), + new NumericElement(8), + ]; + } + + public function testSortASC() : void + { + $newList = BubbleSort::sort($this->list); + self::assertEquals( + [1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } + + public function testSortDESC() : void + { + $newList = BubbleSort::sort($this->list, SortOrder::DESC); + self::assertEquals( + [8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } +} \ No newline at end of file diff --git a/tests/Algorithm/Sort/CocktailShakerSortTest.php b/tests/Algorithm/Sort/CocktailShakerSortTest.php new file mode 100644 index 000000000..459584940 --- /dev/null +++ b/tests/Algorithm/Sort/CocktailShakerSortTest.php @@ -0,0 +1,58 @@ +list = [ + new NumericElement(5), + new NumericElement(1), + new NumericElement(4), + new NumericElement(2), + new NumericElement(8), + ]; + } + + public function testSortASC() : void + { + $newList = CocktailShakerSort::sort($this->list); + self::assertEquals( + [1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } + + public function testSortDESC() : void + { + $newList = CocktailShakerSort::sort($this->list, SortOrder::DESC); + self::assertEquals( + [8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } +} \ No newline at end of file diff --git a/tests/Algorithm/Sort/CombSortTest.php b/tests/Algorithm/Sort/CombSortTest.php new file mode 100644 index 000000000..536b57943 --- /dev/null +++ b/tests/Algorithm/Sort/CombSortTest.php @@ -0,0 +1,58 @@ +list = [ + new NumericElement(5), + new NumericElement(1), + new NumericElement(4), + new NumericElement(2), + new NumericElement(8), + ]; + } + + public function testSortASC() : void + { + $newList = CombSort::sort($this->list); + self::assertEquals( + [1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } + + public function testSortDESC() : void + { + $newList = CombSort::sort($this->list, SortOrder::DESC); + self::assertEquals( + [8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } +} \ No newline at end of file diff --git a/tests/Algorithm/Sort/GnomeSortTest.php b/tests/Algorithm/Sort/GnomeSortTest.php new file mode 100644 index 000000000..9e713f92b --- /dev/null +++ b/tests/Algorithm/Sort/GnomeSortTest.php @@ -0,0 +1,58 @@ +list = [ + new NumericElement(5), + new NumericElement(1), + new NumericElement(4), + new NumericElement(2), + new NumericElement(8), + ]; + } + + public function testSortASC() : void + { + $newList = GnomeSort::sort($this->list); + self::assertEquals( + [1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } + + public function testSortDESC() : void + { + $newList = GnomeSort::sort($this->list, SortOrder::DESC); + self::assertEquals( + [8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } +} \ No newline at end of file diff --git a/tests/Algorithm/Sort/NumericElement.php b/tests/Algorithm/Sort/NumericElement.php new file mode 100644 index 000000000..1db7733db --- /dev/null +++ b/tests/Algorithm/Sort/NumericElement.php @@ -0,0 +1,35 @@ +value = $value; + } + + public function compare(SortableInterface $obj, int $order = SortOrder::ASC) : bool + { + return $order === SortOrder::ASC ? $this->value > $obj->value : $this->value < $obj->value; + } +} \ No newline at end of file diff --git a/tests/Algorithm/Sort/OddEvenSortTest.php b/tests/Algorithm/Sort/OddEvenSortTest.php new file mode 100644 index 000000000..d07828d3c --- /dev/null +++ b/tests/Algorithm/Sort/OddEvenSortTest.php @@ -0,0 +1,58 @@ +list = [ + new NumericElement(5), + new NumericElement(1), + new NumericElement(4), + new NumericElement(2), + new NumericElement(8), + ]; + } + + public function testSortASC() : void + { + $newList = OddEvenSort::sort($this->list); + self::assertEquals( + [1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } + + public function testSortDESC() : void + { + $newList = OddEvenSort::sort($this->list, SortOrder::DESC); + self::assertEquals( + [8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } +} \ No newline at end of file diff --git a/tests/Algorithm/Sort/QuickSortTest.php b/tests/Algorithm/Sort/QuickSortTest.php new file mode 100644 index 000000000..263820b09 --- /dev/null +++ b/tests/Algorithm/Sort/QuickSortTest.php @@ -0,0 +1,58 @@ +list = [ + new NumericElement(5), + new NumericElement(1), + new NumericElement(4), + new NumericElement(2), + new NumericElement(8), + ]; + } + + public function testSortASC() : void + { + $newList = QuickSort::sort($this->list); + self::assertEquals( + [1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } + + public function testSortDESC() : void + { + $newList = QuickSort::sort($this->list, SortOrder::DESC); + self::assertEquals( + [8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } +} \ No newline at end of file diff --git a/tests/Algorithm/Sort/SelectionSortTest.php b/tests/Algorithm/Sort/SelectionSortTest.php new file mode 100644 index 000000000..95979023d --- /dev/null +++ b/tests/Algorithm/Sort/SelectionSortTest.php @@ -0,0 +1,58 @@ +list = [ + new NumericElement(5), + new NumericElement(1), + new NumericElement(4), + new NumericElement(2), + new NumericElement(8), + ]; + } + + public function testSortASC() : void + { + $newList = SelectionSort::sort($this->list); + self::assertEquals( + [1, 2, 4, 5, 8], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } + + public function testSortDESC() : void + { + $newList = SelectionSort::sort($this->list, SortOrder::DESC); + self::assertEquals( + [8, 5, 4, 2, 1], [$newList[0]->value, $newList[1]->value, $newList[2]->value, $newList[3]->value, $newList[4]->value,] + ); + } +} \ No newline at end of file