归并排序
来源:互联网 发布:dz论坛seo 编辑:程序博客网 时间:2024/05/16 16:15
$input = array(6, 5, 3, 1, 8, 7, 2, 4);
function merge_sort($arr)
{
if (count($arr) <= 1)
{
return $arr;
}
$left = array_slice($arr, 0, (int)(count($arr)/2));
$right = array_slice($arr, (int)(count($arr)/2));
$left = merge_sort($left);
$right = merge_sort($right);
$output = merge($left, $right);
return $output;
}
function merge($left, $right)
{
$result = array();
while (count($left) > 0 && count($right) > 0)
{
if ($left[0] <= $right[0])
{
array_push($result,
array_shift($left));
}
else
{
array_push($result,
array_shift($right));
}
}
array_splice($result, count($result), 0, $left);
array_splice($result, count($result), 0, $right);
return $result;
}
// 1, 2, 3, 4, 5, 6, 7, 8
$output = merge_sort($input);
var_dump($output);
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 百度分享
- Java 基础拾遗
- Web api dynamic
- ubuntu14.04设置静态ip
- 经典算法与数据结构的c++实现——冒泡排序
- 归并排序
- FMDatabase 数据库的使用
- 获取class的对象结果集 getClass 方法,附运用源码
- 用VPN提高外网访问速度的方法
- 地籍图宗记注记标注实现
- 一个简单的ajax+servlert+jsp的完整实例
- 消息队列
- 科普:alphago是什么
- 设计模式(三)观察者模式Observer(发布订阅)