Skip to content

Commit bc91613

Browse files
committed
improvements in sorting algorithms, new class InsertionSort, minor fixes and improvements
1 parent 0a65081 commit bc91613

File tree

8 files changed

+153
-18
lines changed

8 files changed

+153
-18
lines changed

src/Algorithm/Sorting/BubbleSort.php

+4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ class BubbleSort implements ISortable {
4141
* @return array
4242
*/
4343
public function sort(array $array): array {
44+
$array = \array_values($array);
4445
$length = \count($array);
46+
47+
if (0 === $length || 1 === $length) return $array;
48+
4549
for ($i = 0; $i < $length; $i++) {
4650
for ($j = 0; $j < $length - $i - 1; $j++) {
4751
if (Comparator::greaterThan($array[$j], $array[$j + 1])) {
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
use doganoo\PHPAlgorithms\Common\Util\Comparator;
31+
32+
/**
33+
* Class InsertionSort
34+
*
35+
* @package doganoo\PHPAlgorithms\Algorithm\Sorting
36+
*/
37+
class InsertionSort implements ISortable {
38+
39+
/**
40+
* @param array $array
41+
* @return array
42+
*/
43+
public function sort(array $array): array {
44+
$array = \array_values($array);
45+
$size = \count($array);
46+
47+
if (0 === $size || 1 === $size) return $array;
48+
49+
for ($i = 1; $i < $size; $i++) {
50+
$j = $i;
51+
while (($j > 0) && (Comparator::lessThan($array[$j], $array[$j - 1]))) {
52+
$tmp = $array[$j - 1];
53+
$array[$j - 1] = $array[$j];
54+
$array[$j] = $tmp;
55+
$j--;
56+
}
57+
}
58+
return $array;
59+
}
60+
}

src/Algorithm/Sorting/MergeSort.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ class MergeSort implements ISortable {
4242
* @return array
4343
*/
4444
public function sort(array $array): array {
45+
$array = \array_values($array);
4546
$arraySize = count($array);
46-
if ($arraySize == 1) {
47-
return $array;
48-
}
47+
48+
if (0 === $arraySize || $arraySize == 1) return $array;
49+
4950
$middle = floor($arraySize / 2);
5051
$left = array_slice($array, 0, $middle);
5152
$right = array_slice($array, $middle);
@@ -60,13 +61,14 @@ public function sort(array $array): array {
6061
* @return array
6162
*/
6263
private function merge(?array $left, ?array $right): array {
63-
if ($left == null) {
64-
return [];
65-
}
66-
if ($right == null) {
67-
return [];
68-
}
64+
65+
if (null === $left) return [];
66+
if (null === $right) return [];
67+
6968
$result = [];
69+
$left = \array_values($left);
70+
$right = \array_values($right);
71+
7072
while (count($left) !== 0 && count($right) !== 0) {
7173
if (Comparator::greaterThan($left[0], $right[0])) {
7274
$result[] = $right[0];

src/Algorithm/Sorting/QuickSort.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,29 @@ class QuickSort implements ISortable {
4040
* @return array
4141
*/
4242
public function sort(array $array): array {
43+
$array = \array_values($array);
4344
$arraySize = count($array);
44-
if ($arraySize == 1) {
45-
return $array;
46-
}
4745

48-
$pivot = $array[1];
46+
if (0 === $arraySize || $arraySize == 1) return $array;
47+
48+
$pivot = $array[1]; //TODO how to choose the best pivot?!
4949
$left = [];
5050
$right = [];
5151

5252
while (count($array) !== 0) {
53-
$key = key($array);
54-
$value = current($array);
55-
unset($array[$key]);
53+
$value = $array[0];
5654

5755
if (Comparator::lessThan($value, $pivot)) {
5856
$left[] = $value;
5957
} else {
6058
$right[] = $value;
6159
}
60+
$array = \array_slice($array, 1);
6261
}
6362
return array_merge(
6463
$this->sort($left)
6564
, [$pivot]
6665
, $this->sort($right)
6766
);
6867
}
69-
70-
7168
}

src/Algorithm/Sorting/SelectionSort.php

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class SelectionSort implements ISortable {
4747
public function sort(array $array): array {
4848
$array = \array_values($array);
4949
$length = \count($array);
50+
51+
if (0 === $length || 1 === $length) return $array;
52+
5053
for ($i = 0; $i < $length; $i++) {
5154
$min = $i;
5255
for ($j = $i; $j < $length; $j++) {

src/Datastructure/Maps/HashMap.php

+14
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,18 @@ public function keySet(): array {
380380
}
381381
return $keySet;
382382
}
383+
384+
/**
385+
* @return array
386+
*/
387+
public function countPerBucket() {
388+
$i = 0;
389+
$array = [];
390+
/** @var SinglyLinkedList $list */
391+
foreach ($this->bucket as $list) {
392+
$array[$i] = $list->size();
393+
$i++;
394+
}
395+
return $array;
396+
}
383397
}

tests/Lists/ArrayLists/ArrayListTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,52 @@ public function testIterator() {
171171
$this->assertTrue($i === $arrayList->length());
172172
}
173173

174+
public function testSerialize() {
175+
$arrayList = new ArrayList();
176+
$x = new class {
177+
public $id;
178+
};
179+
$x->id = 1;
180+
$arrayList->add($x);
181+
$x->id = 2;
182+
$arrayList->add($x);
183+
$x->id = 3;
184+
$arrayList->add($x);
185+
$x->id = 4;
186+
$arrayList->add($x);
187+
$x->id = 5;
188+
$arrayList->add($x);
189+
$x->id = 6;
190+
$arrayList->add($x);
191+
$x->id = 7;
192+
$arrayList->add($x);
193+
$x->id = 8;
194+
$arrayList->add($x);
195+
$x->id = 9;
196+
$arrayList->add($x);
197+
$x->id = 10;
198+
$arrayList->add($x);
199+
$x->id = 11;
200+
$arrayList->add($x);
201+
$x->id = 12;
202+
$arrayList->add($x);
203+
$x->id = 13;
204+
$arrayList->add($x);
205+
$x->id = 14;
206+
$arrayList->add($x);
207+
$x->id = 15;
208+
$arrayList->add($x);
209+
$x->id = 16;
210+
$arrayList->add($x);
211+
$x->id = 17;
212+
$arrayList->add($x);
213+
214+
$json = json_encode($arrayList);
215+
$this->assertTrue(false !== is_string($json));
216+
217+
$list = json_decode($json);
218+
219+
$this->assertTrue(null !== $list && $list instanceof stdClass);
220+
}
221+
174222
}

tests/Sorting/SortTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626

2727
use doganoo\PHPAlgorithms\Algorithm\Sorting\BubbleSort;
28+
use doganoo\PHPAlgorithms\Algorithm\Sorting\InsertionSort;
2829
use doganoo\PHPAlgorithms\Algorithm\Sorting\MergeSort;
2930
use doganoo\PHPAlgorithms\Algorithm\Sorting\SelectionSort;
3031

@@ -50,4 +51,10 @@ public function testMergeSort() {
5051
$result = $bubbleSort->sort($arr);
5152
$this->assertTrue($result === [1, 9, 12, 13, 40, 55]);
5253
}
54+
public function testInsertionSort() {
55+
$bubbleSort = new InsertionSort();
56+
$arr = [12, 40, 9, 55, 1, 13];
57+
$result = $bubbleSort->sort($arr);
58+
$this->assertTrue($result === [1, 9, 12, 13, 40, 55]);
59+
}
5360
}

0 commit comments

Comments
 (0)