diff --git a/src/Algorithm/Sorting/BubbleSort.php b/src/Algorithm/Sorting/BubbleSort.php index dce7d68..33bd603 100644 --- a/src/Algorithm/Sorting/BubbleSort.php +++ b/src/Algorithm/Sorting/BubbleSort.php @@ -42,12 +42,13 @@ class BubbleSort implements ISortable { */ public function sort(array $array): array { $array = \array_values($array); - $length = \count($array); + $size = \count($array); - if (0 === $length || 1 === $length) return $array; + if (0 === $size) return []; + if (1 === $size) return $array; - for ($i = 0; $i < $length; $i++) { - for ($j = 0; $j < $length - $i - 1; $j++) { + for ($i = 0; $i < $size; $i++) { + for ($j = 0; $j < $size - $i - 1; $j++) { if (Comparator::greaterThan($array[$j], $array[$j + 1])) { $tmp = $array[$j]; $array[$j] = $array[$j + 1]; diff --git a/src/Algorithm/Sorting/InsertionSort.php b/src/Algorithm/Sorting/InsertionSort.php index dd38630..101cfb4 100644 --- a/src/Algorithm/Sorting/InsertionSort.php +++ b/src/Algorithm/Sorting/InsertionSort.php @@ -44,7 +44,8 @@ public function sort(array $array): array { $array = \array_values($array); $size = \count($array); - if (0 === $size || 1 === $size) return $array; + if (0 === $size) return []; + if (1 === $size) return $array; for ($i = 1; $i < $size; $i++) { $j = $i; diff --git a/src/Algorithm/Sorting/MergeSort.php b/src/Algorithm/Sorting/MergeSort.php index c6dd58c..d63e0ad 100644 --- a/src/Algorithm/Sorting/MergeSort.php +++ b/src/Algorithm/Sorting/MergeSort.php @@ -45,7 +45,8 @@ public function sort(array $array): array { $array = \array_values($array); $arraySize = count($array); - if (0 === $arraySize || $arraySize == 1) return $array; + if (0 === $arraySize) return []; + if ($arraySize == 1) return $array; $middle = floor($arraySize / 2); $left = array_slice($array, 0, $middle); diff --git a/src/Algorithm/Sorting/QuickSort.php b/src/Algorithm/Sorting/QuickSort.php index 8b70073..1c46895 100644 --- a/src/Algorithm/Sorting/QuickSort.php +++ b/src/Algorithm/Sorting/QuickSort.php @@ -41,28 +41,26 @@ class QuickSort implements ISortable { */ public function sort(array $array): array { $array = \array_values($array); - $arraySize = count($array); + $size = \count($array); - if (0 === $arraySize || $arraySize == 1) return $array; + if ($size <= 1) return $array; - $pivot = $array[1]; //TODO how to choose the best pivot?! - $left = []; - $right = []; + $pivot = $array[0]; + $left = $right = []; - while (count($array) !== 0) { - $value = $array[0]; - - if (Comparator::lessThan($value, $pivot)) { - $left[] = $value; + for ($i = 1; $i < count($array); $i++) { + if (Comparator::lessThan($array[$i], $pivot)) { + $left[] = $array[$i]; } else { - $right[] = $value; + $right[] = $array[$i]; } - $array = \array_slice($array, 1); } + return array_merge( $this->sort($left) , [$pivot] , $this->sort($right) ); } + } \ No newline at end of file diff --git a/src/Algorithm/Sorting/TimSort.php b/src/Algorithm/Sorting/TimSort.php new file mode 100644 index 0000000..3849b4f --- /dev/null +++ b/src/Algorithm/Sorting/TimSort.php @@ -0,0 +1,56 @@ +sort(\array_slice($array, $i, min(($i + self::RUN), ($size)))); + $arr = $mergeSort->sort($arr); + $result = \array_merge($result, $arr); + } + return $result; + } +} \ No newline at end of file diff --git a/tests/Sorting/SortTest.php b/tests/Sorting/SortTest.php index 99e3a76..f2bc8c0 100644 --- a/tests/Sorting/SortTest.php +++ b/tests/Sorting/SortTest.php @@ -27,7 +27,9 @@ use doganoo\PHPAlgorithms\Algorithm\Sorting\BubbleSort; use doganoo\PHPAlgorithms\Algorithm\Sorting\InsertionSort; use doganoo\PHPAlgorithms\Algorithm\Sorting\MergeSort; +use doganoo\PHPAlgorithms\Algorithm\Sorting\QuickSort; use doganoo\PHPAlgorithms\Algorithm\Sorting\SelectionSort; +use doganoo\PHPAlgorithms\Algorithm\Sorting\TimSort; /** * Class SortTest @@ -37,24 +39,95 @@ public function testBubbleSort() { $bubbleSort = new BubbleSort(); $result = $bubbleSort->sort([12, 40, 9, 55, 1, 13]); $this->assertTrue($result === [1, 9, 12, 13, 40, 55]); + + $result = $bubbleSort->sort([]); + $this->assertTrue($result === []); + + $result = $bubbleSort->sort([9]); + $this->assertTrue($result === [9]); } public function testSelectionSort() { - $bubbleSort = new SelectionSort(); - $result = $bubbleSort->sort([12, 40, 9, 55, 1, 13]); + $selectionSort = new SelectionSort(); + $result = $selectionSort->sort([12, 40, 9, 55, 1, 13]); $this->assertTrue($result === [1, 9, 12, 13, 40, 55]); + + $result = $selectionSort->sort([]); + $this->assertTrue($result === []); + + $result = $selectionSort->sort([9]); + $this->assertTrue($result === [9]); } public function testMergeSort() { - $bubbleSort = new MergeSort(); + $mergeSort = new MergeSort(); $arr = [12, 40, 9, 55, 1, 13]; - $result = $bubbleSort->sort($arr); + $result = $mergeSort->sort($arr); $this->assertTrue($result === [1, 9, 12, 13, 40, 55]); + + $result = $mergeSort->sort([]); + $this->assertTrue($result === []); + + $result = $mergeSort->sort([9]); + $this->assertTrue($result === [9]); } + public function testInsertionSort() { - $bubbleSort = new InsertionSort(); + $insertionSort = new InsertionSort(); + $arr = [12, 40, 9, 55, 1, 13]; + $result = $insertionSort->sort($arr); + $this->assertTrue($result === [1, 9, 12, 13, 40, 55]); + + $result = $insertionSort->sort([]); + $this->assertTrue($result === []); + + $result = $insertionSort->sort([9]); + $this->assertTrue($result === [9]); + } + + public function testTimSort() { + $timSort = new TimSort(); $arr = [12, 40, 9, 55, 1, 13]; - $result = $bubbleSort->sort($arr); + $result = $timSort->sort($arr); $this->assertTrue($result === [1, 9, 12, 13, 40, 55]); + + $arr = [5, 21, 7, 23, 19]; + $result = $timSort->sort($arr); + $this->assertTrue($result === [5, 7, 19, 21, 23]); + + $arr = [2, 3, 1, 5, 6, 7]; + $result = $timSort->sort($arr); + $this->assertTrue($result === [1, 2, 3, 5, 6, 7]); + + $arr = []; + $result = $timSort->sort($arr); + $this->assertTrue($result === []); + + $arr = [1]; + $result = $timSort->sort($arr); + $this->assertTrue($result === [1]); + } + + public function testQuickSort() { + $quickSort = new QuickSort(); + $arr = [12, 40, 9, 55, 1, 13]; + $result = $quickSort->sort($arr); + $this->assertTrue($result === [1, 9, 12, 13, 40, 55]); + + $arr = [5, 21, 7, 23, 19]; + $result = $quickSort->sort($arr); + $this->assertTrue($result === [5, 7, 19, 21, 23]); + + $arr = [2, 3, 1, 5, 6, 7]; + $result = $quickSort->sort($arr); + $this->assertTrue($result === [1, 2, 3, 5, 6, 7]); + + $arr = []; + $result = $quickSort->sort($arr); + $this->assertTrue($result === []); + + $arr = [1]; + $result = $quickSort->sort($arr); + $this->assertTrue($result === [1]); } } \ No newline at end of file