-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge.php
More file actions
74 lines (65 loc) · 1.76 KB
/
Merge.php
File metadata and controls
74 lines (65 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* Created by PhpStorm.
* User: murat
* Date: 10.12.18
* Time: 20:56
*
* Original example - https://www.w3resource.com/php-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-17.php
*/
class Merge
{
protected $array;
public function __construct($array)
{
$this->array = $array;
}
public function splice($array)
{
//If this is last element
if (count($array) == 1) {
return $array;
}
//Explode array
$middle = count($array) / 2;
$left = array_slice($array, 0, $middle);
$right = array_slice($array, $middle);
//Recursive sort
$left = $this->splice($left);
$right = $this->splice($right);
//Merge and return
return $this->merge($left, $right);
}
public function merge($left, $right)
{
$result = array();
while (count($left) > 0 && count($right) > 0) {
if ($left[0] > $right[0]) {
$result[] = $right[0];
$right = array_slice($right, 1);
} else {
$result[] = $left[0];
$left = array_slice($left, 1);
}
}
while (count($left) > 0) {
$result[] = $left[0];
$left = array_slice($left, 1);
}
while (count($right) > 0) {
$result[] = $right[0];
$right = array_slice($right, 1);
}
return $result;
}
public function getArray()
{
return $this->splice($this->array);
}
}
$test_array = array(100, 54, 7, 2, 5, 4, 1);
echo "Original Array : ";
echo implode(', ', $test_array);
echo "\nSorted Array :";
$merge = new Merge($test_array);
echo implode(', ', $merge->getArray()) . "\n";