/
MergeSort.php
51 lines (42 loc) · 1.32 KB
/
MergeSort.php
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
<?php
$before = [];
for ($i = 0; $i < rand(1, 50); $i++) {
$before[] = rand(-100, 100);
}
echo '<h3>Before:</h3>';
echo json_encode($before);
echo '<hr/><h3>Steps:</h3>';
$after = mergeSort($before);
echo '<hr/><h3>After:</h3>';
echo json_encode($after);
function mergeSort($array)
{
if (count($array) < 2) {
return $array;
}
$mid = count($array) / 2;
echo '<h5>Splitting '.json_encode($array).' ===> Left: '.json_encode(array_slice($array, 0, $mid)).' Right: '.json_encode(array_slice($array, $mid));
$right = mergeSort(array_slice($array, 0, $mid));
$left = mergeSort(array_slice($array, $mid));
return merge($left, $right);
}
function merge($left, $right)
{
echo '<h5>Merging '.json_encode($left).' and '.json_encode($right).' ===> Result: ';
$result = [];
while (count($left) > 0 || count($right) > 0) {
if (count($left) > 0 && count($right) > 0) {
if ($left[0] < $right[0]) {
array_push($result, array_shift($left));
} else {
array_push($result, array_shift($right));
}
} elseif (count($left) > 0) {
array_push($result, array_shift($left));
} else {
array_push($result, array_shift($right));
}
}
echo json_encode($result).'</h5>';
return $result;
}