Skip to content

Commit e11d2fc

Browse files
committed
TimSort and sort test
1 parent 96a5d66 commit e11d2fc

File tree

6 files changed

+154
-24
lines changed

6 files changed

+154
-24
lines changed

src/Algorithm/Sorting/BubbleSort.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ class BubbleSort implements ISortable {
4242
*/
4343
public function sort(array $array): array {
4444
$array = \array_values($array);
45-
$length = \count($array);
45+
$size = \count($array);
4646

47-
if (0 === $length || 1 === $length) return $array;
47+
if (0 === $size) return [];
48+
if (1 === $size) return $array;
4849

49-
for ($i = 0; $i < $length; $i++) {
50-
for ($j = 0; $j < $length - $i - 1; $j++) {
50+
for ($i = 0; $i < $size; $i++) {
51+
for ($j = 0; $j < $size - $i - 1; $j++) {
5152
if (Comparator::greaterThan($array[$j], $array[$j + 1])) {
5253
$tmp = $array[$j];
5354
$array[$j] = $array[$j + 1];

src/Algorithm/Sorting/InsertionSort.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public function sort(array $array): array {
4444
$array = \array_values($array);
4545
$size = \count($array);
4646

47-
if (0 === $size || 1 === $size) return $array;
47+
if (0 === $size) return [];
48+
if (1 === $size) return $array;
4849

4950
for ($i = 1; $i < $size; $i++) {
5051
$j = $i;

src/Algorithm/Sorting/MergeSort.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public function sort(array $array): array {
4545
$array = \array_values($array);
4646
$arraySize = count($array);
4747

48-
if (0 === $arraySize || $arraySize == 1) return $array;
48+
if (0 === $arraySize) return [];
49+
if ($arraySize == 1) return $array;
4950

5051
$middle = floor($arraySize / 2);
5152
$left = array_slice($array, 0, $middle);

src/Algorithm/Sorting/QuickSort.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,26 @@ class QuickSort implements ISortable {
4141
*/
4242
public function sort(array $array): array {
4343
$array = \array_values($array);
44-
$arraySize = count($array);
44+
$size = \count($array);
4545

46-
if (0 === $arraySize || $arraySize == 1) return $array;
46+
if ($size <= 1) return $array;
4747

48-
$pivot = $array[1]; //TODO how to choose the best pivot?!
49-
$left = [];
50-
$right = [];
48+
$pivot = $array[0];
49+
$left = $right = [];
5150

52-
while (count($array) !== 0) {
53-
$value = $array[0];
54-
55-
if (Comparator::lessThan($value, $pivot)) {
56-
$left[] = $value;
51+
for ($i = 1; $i < count($array); $i++) {
52+
if (Comparator::lessThan($array[$i], $pivot)) {
53+
$left[] = $array[$i];
5754
} else {
58-
$right[] = $value;
55+
$right[] = $array[$i];
5956
}
60-
$array = \array_slice($array, 1);
6157
}
58+
6259
return array_merge(
6360
$this->sort($left)
6461
, [$pivot]
6562
, $this->sort($right)
6663
);
6764
}
65+
6866
}

src/Algorithm/Sorting/TimSort.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
namespace doganoo\PHPAlgorithms\Algorithm\Sorting;
27+
28+
29+
use doganoo\PHPAlgorithms\Common\Interfaces\ISortable;
30+
31+
class TimSort implements ISortable {
32+
public const RUN = 32;
33+
34+
/**
35+
* @param array $array
36+
* @return array
37+
*/
38+
public function sort(array $array): array {
39+
$array = \array_values($array);
40+
$size = \count($array);
41+
42+
if (0 === $size) return [];
43+
if (1 === $size) return $array;
44+
45+
$insertionSort = new InsertionSort();
46+
$mergeSort = new MergeSort();
47+
48+
$result = [];
49+
for ($i = 0; $i < $size; $i = $i + self::RUN) {
50+
$arr = $insertionSort->sort(\array_slice($array, $i, min(($i + self::RUN), ($size))));
51+
$arr = $mergeSort->sort($arr);
52+
$result = \array_merge($result, $arr);
53+
}
54+
return $result;
55+
}
56+
}

tests/Sorting/SortTest.php

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
use doganoo\PHPAlgorithms\Algorithm\Sorting\BubbleSort;
2828
use doganoo\PHPAlgorithms\Algorithm\Sorting\InsertionSort;
2929
use doganoo\PHPAlgorithms\Algorithm\Sorting\MergeSort;
30+
use doganoo\PHPAlgorithms\Algorithm\Sorting\QuickSort;
3031
use doganoo\PHPAlgorithms\Algorithm\Sorting\SelectionSort;
32+
use doganoo\PHPAlgorithms\Algorithm\Sorting\TimSort;
3133

3234
/**
3335
* Class SortTest
@@ -37,24 +39,95 @@ public function testBubbleSort() {
3739
$bubbleSort = new BubbleSort();
3840
$result = $bubbleSort->sort([12, 40, 9, 55, 1, 13]);
3941
$this->assertTrue($result === [1, 9, 12, 13, 40, 55]);
42+
43+
$result = $bubbleSort->sort([]);
44+
$this->assertTrue($result === []);
45+
46+
$result = $bubbleSort->sort([9]);
47+
$this->assertTrue($result === [9]);
4048
}
4149

4250
public function testSelectionSort() {
43-
$bubbleSort = new SelectionSort();
44-
$result = $bubbleSort->sort([12, 40, 9, 55, 1, 13]);
51+
$selectionSort = new SelectionSort();
52+
$result = $selectionSort->sort([12, 40, 9, 55, 1, 13]);
4553
$this->assertTrue($result === [1, 9, 12, 13, 40, 55]);
54+
55+
$result = $selectionSort->sort([]);
56+
$this->assertTrue($result === []);
57+
58+
$result = $selectionSort->sort([9]);
59+
$this->assertTrue($result === [9]);
4660
}
4761

4862
public function testMergeSort() {
49-
$bubbleSort = new MergeSort();
63+
$mergeSort = new MergeSort();
5064
$arr = [12, 40, 9, 55, 1, 13];
51-
$result = $bubbleSort->sort($arr);
65+
$result = $mergeSort->sort($arr);
5266
$this->assertTrue($result === [1, 9, 12, 13, 40, 55]);
67+
68+
$result = $mergeSort->sort([]);
69+
$this->assertTrue($result === []);
70+
71+
$result = $mergeSort->sort([9]);
72+
$this->assertTrue($result === [9]);
5373
}
74+
5475
public function testInsertionSort() {
55-
$bubbleSort = new InsertionSort();
76+
$insertionSort = new InsertionSort();
77+
$arr = [12, 40, 9, 55, 1, 13];
78+
$result = $insertionSort->sort($arr);
79+
$this->assertTrue($result === [1, 9, 12, 13, 40, 55]);
80+
81+
$result = $insertionSort->sort([]);
82+
$this->assertTrue($result === []);
83+
84+
$result = $insertionSort->sort([9]);
85+
$this->assertTrue($result === [9]);
86+
}
87+
88+
public function testTimSort() {
89+
$timSort = new TimSort();
5690
$arr = [12, 40, 9, 55, 1, 13];
57-
$result = $bubbleSort->sort($arr);
91+
$result = $timSort->sort($arr);
5892
$this->assertTrue($result === [1, 9, 12, 13, 40, 55]);
93+
94+
$arr = [5, 21, 7, 23, 19];
95+
$result = $timSort->sort($arr);
96+
$this->assertTrue($result === [5, 7, 19, 21, 23]);
97+
98+
$arr = [2, 3, 1, 5, 6, 7];
99+
$result = $timSort->sort($arr);
100+
$this->assertTrue($result === [1, 2, 3, 5, 6, 7]);
101+
102+
$arr = [];
103+
$result = $timSort->sort($arr);
104+
$this->assertTrue($result === []);
105+
106+
$arr = [1];
107+
$result = $timSort->sort($arr);
108+
$this->assertTrue($result === [1]);
109+
}
110+
111+
public function testQuickSort() {
112+
$quickSort = new QuickSort();
113+
$arr = [12, 40, 9, 55, 1, 13];
114+
$result = $quickSort->sort($arr);
115+
$this->assertTrue($result === [1, 9, 12, 13, 40, 55]);
116+
117+
$arr = [5, 21, 7, 23, 19];
118+
$result = $quickSort->sort($arr);
119+
$this->assertTrue($result === [5, 7, 19, 21, 23]);
120+
121+
$arr = [2, 3, 1, 5, 6, 7];
122+
$result = $quickSort->sort($arr);
123+
$this->assertTrue($result === [1, 2, 3, 5, 6, 7]);
124+
125+
$arr = [];
126+
$result = $quickSort->sort($arr);
127+
$this->assertTrue($result === []);
128+
129+
$arr = [1];
130+
$result = $quickSort->sort($arr);
131+
$this->assertTrue($result === [1]);
59132
}
60133
}

0 commit comments

Comments
 (0)