File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
src/Datastructure/Lists/ArrayLists Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 26
26
namespace doganoo \PHPAlgorithms \Datastructure \Lists \ArrayLists ;
27
27
28
28
29
+ use doganoo \PHPAlgorithms \Algorithm \Sorting \TimSort ;
29
30
use doganoo \PHPAlgorithms \Common \Exception \IndexOutOfBoundsException ;
30
31
use doganoo \PHPAlgorithms \Common \Interfaces \IComparable ;
31
32
use doganoo \PHPAlgorithms \Common \Util \Comparator ;
@@ -426,4 +427,20 @@ public function compareTo($object): int {
426
427
}
427
428
return -1 ;
428
429
}
430
+
431
+ /**
432
+ * @return bool
433
+ */
434
+ public function sort (): bool {
435
+ $ array = \array_filter ($ this ->array , function ($ value , $ key ) {
436
+ return $ value !== null ;
437
+ }, \ARRAY_FILTER_USE_BOTH );
438
+
439
+
440
+ $ timSort = new TimSort ();
441
+ $ array = $ timSort ->sort ($ array );
442
+ $ this ->array = \array_fill (0 , self ::DEFAULT_CAPACITY , null );
443
+ $ this ->addAllArray ($ array );
444
+ return true ;
445
+ }
429
446
}
Original file line number Diff line number Diff line change @@ -218,4 +218,21 @@ public function testSerialize() {
218
218
$ this ->assertTrue (null !== $ list && $ list instanceof stdClass);
219
219
}
220
220
221
+ public function testSort () {
222
+ $ arrayList = new ArrayList ();
223
+ $ arrayList ->add (3 );
224
+ $ arrayList ->add (2 );
225
+ $ arrayList ->add (5 );
226
+ $ arrayList ->add (1 );
227
+ $ arrayList ->add (4 );
228
+
229
+ $ arrayList ->sort ();
230
+
231
+ $ this ->assertTrue (1 === $ arrayList ->get (0 ));
232
+ $ this ->assertTrue (2 === $ arrayList ->get (1 ));
233
+ $ this ->assertTrue (3 === $ arrayList ->get (2 ));
234
+ $ this ->assertTrue (4 === $ arrayList ->get (3 ));
235
+ $ this ->assertTrue (5 === $ arrayList ->get (4 ));
236
+ }
237
+
221
238
}
You can’t perform that action at this time.
0 commit comments