Skip to content

Commit

Permalink
TimSort and sort test
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Nov 29, 2018
1 parent 96a5d66 commit e11d2fc
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 24 deletions.
9 changes: 5 additions & 4 deletions src/Algorithm/Sorting/BubbleSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
3 changes: 2 additions & 1 deletion src/Algorithm/Sorting/InsertionSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/Algorithm/Sorting/MergeSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 10 additions & 12 deletions src/Algorithm/Sorting/QuickSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

}
56 changes: 56 additions & 0 deletions src/Algorithm/Sorting/TimSort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* MIT License
*
* Copyright (c) 2018 Dogan Ucar
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace doganoo\PHPAlgorithms\Algorithm\Sorting;


use doganoo\PHPAlgorithms\Common\Interfaces\ISortable;

class TimSort implements ISortable {
public const RUN = 32;

/**
* @param array $array
* @return array
*/
public function sort(array $array): array {
$array = \array_values($array);
$size = \count($array);

if (0 === $size) return [];
if (1 === $size) return $array;

$insertionSort = new InsertionSort();
$mergeSort = new MergeSort();

$result = [];
for ($i = 0; $i < $size; $i = $i + self::RUN) {
$arr = $insertionSort->sort(\array_slice($array, $i, min(($i + self::RUN), ($size))));
$arr = $mergeSort->sort($arr);
$result = \array_merge($result, $arr);
}
return $result;
}
}
85 changes: 79 additions & 6 deletions tests/Sorting/SortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);
}
}

0 comments on commit e11d2fc

Please sign in to comment.