-
Notifications
You must be signed in to change notification settings - Fork 88
/
mergesort.php
44 lines (38 loc) · 999 Bytes
/
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
<?php
function mergesort(array $a) {
if (count($a) == 1) {
return $a;
} elseif (count($a) == 2) {
if ($a[0] > $a[1]) {
return [$a[1], $a[0]];
}
return $a;
}
$p = (int) (count($a) / 2);
$m1 = mergesort(array_slice($a, 0, $p));
$m2 = mergesort(array_slice($a, $p));
$ret = [];
while (1) {
if (count($m1) > 0 && count($m2) > 0) {
if ($m1[0] <= $m2[0]) {
$ret[] = $m1[0];
$m1 = array_slice($m1, 1);
} else {
$ret[] = $m2[0];
$m2 = array_slice($m2, 1);
}
} else if (count($m1) > 0) {
$ret = array_merge($ret, $m1);
$m1 = [];
} else if (count($m2) > 0) {
$ret = array_merge($ret, $m2);
$m2 = [];
} else {
break;
}
}
return $ret;
}
$a = [5,9,1,3,4,6,6,3,2];
$r = mergesort($a);
echo sprintf("%s\n", json_encode($r));