还是要常常写程序啊,顺序表的归并操作,是归并排序中的基础操作
<?php class Merge{ /* * 顺序表合并操作 */ public function mergeArray($a,$b){ $len_a = count($a); $len_b = count($b); $i = 0; $j = 0; while($i<$len_a&&$j<$len_b){ if($a[$i]<$b[$j]){ $tmp[] = $a[$i++]; }else { $tmp[] = $b[$j++]; } } while ($i<$len_a){ $tmp[] = $a[$i++]; } while ($j<$len_b){ $tmp[] = $b[$j++]; } return $tmp; } } class Client{ public static function main(){ $arr_1 = array(1,3,4,5,7,9); $arr_2 = array(2,3,4,6,8,10); $obj = new Merge(); $arr = $obj->mergeArray($arr_1, $arr_2); print_r($arr); } } Client::main(); ?>
时间: 2024-11-07 15:02:12