-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharray_merge_sort.php
55 lines (44 loc) · 1.3 KB
/
array_merge_sort.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
52
53
54
55
<?php
/**
* 合并两个有序数组为一个有序数组
* Created by PhpStorm.
* User: chentao
* Date: 2019/1/19
* Time: 2:33 PM
*/
$a = [1,3,5,7,9];
$b = [2,4,6];
/**
* @param $a
* @param $b
* @return array
* 时间复杂度 O(n^2)
*/
function sort_mix_array($a, $b)
{
if(count($a) == 0) return $b;
$result = [];
$i=0;
$b_cnt = count($b) - 1;
//以a数组中的元素为基数,将b中比基数小的先存入结果数组,再将基数存入;
//再取出a中的下一个数对b剩下的数组重复以上操作,直到所有元素存入结果数组
foreach ($a as $k => $item) {
while ($item > $b[$i]) {
$result[] = $b[$i];
$i++;
// b数组中的元素已经全部进入新数组中,则将a数组中剩余的部分追加进结果数组
if ($i>$b_cnt) {
$result = array_merge($result, $a);
// 注意不能用 array + array 合并数组。因为以数字为key,相同key的元素会被忽略
//$result += $a;
return $result;
}
}
$result[] = $item;
unset($a[$k]);
}
return $result;
}
print_r(sort_mix_array($a, $b));
//还有改造空间,在对b的遍历可以使用二分查找
//时间复杂度降到 O(nlogn)