分块查找算法 (php)
2017-01-21 21:09
357 查看
class Node { public $data; // 数据域 public $key; // 关键之查找 } class IndexNode { public $key; // 记录当前快中最大的key数 public $link; // 当地块的起始地址 } function InitIndex(SplFixedArray $seqList) { $n = $seqList->count(); $s = ceil(sqrt($n)); $b = $s; $indexList = new SplFixedArray($b); for ($i = 0; $i < $s; $i ++) { $max = null; for ($j = 0; $j < $s; $j ++) { if ($i * $s + $j < $n && $max < $seqList[$i * $s + $j]->key) { $max = $seqList[$i * $s + $j]->key; } } $idx = new IndexNode(); $idx->key = $max; $idx->link = $i * $s; $indexList[$i] = $idx; } return $indexList; } function searchIndex(SplFixedArray $seqList, SplFixedArray $indexList, $val) { $n = $seqList->count(); $m = ceil(sqrt($n)); for ($i = 0; $i < $m; $i ++) { if ($seqList[$indexList[$i]->key]->data < $val) continue; $j = $indexList[$i]->link; $end = $j + $m; while ($j <= $end && $seqList[$j]->data != $val) { $j ++; } if ($j > $end) { continue; } else { return $j; } } return -1; } $arr=[14,27,32,50,10,76,32,55,44,33,9,96,30,57,49]; $seqList = new SplFixedArray(count($arr)); for($i=0;$i<count($arr);$i++){ $node=new Node(); $node->key=$i; $node->data=$arr[$i]; $seqList[$i]=$node; } $indexList = InitIndex($seqList); $index=searchIndex($seqList, $indexList, 55); print_r($index);
相关文章推荐
- PHP算法之二分查找和顺序查找
- PHP 顺序查找和二分查找(也叫做折半查找)算法
- [php] 数据结构&算法(PHP描述) 查找&&二分法查找
- PHP算法之二分查找和顺序查找
- php关于二分查找的算法
- 使用PHP描述顺序查找和二分查找(也叫做折半查找)算法,顺序查找必须考虑效率,对象可以是一个有序数组
- php 基础算法之 二分查找
- 使用PHP描述顺序查找和二分查找(也叫做折半查找)算法,顺序查找必须考虑效率,对象可以是一个有序数组
- PHP:冒泡排序、快速查找和二分查找的思路以及算法
- PHP实现常见算法之二分查找法
- php中二分法查找算法实例分析
- PHP有序表查找之二分查找(折半查找)算法示例
- php 二分算法查找
- php二分查找、顺序查找算法
- PHP算法面试题 排序和查找
- 连续整数之和为1000的共有几组算法及php执行时间和内存占用小例
- 在网上看到使用PHP实现常见的数据结构和算法
- 在PHP中使用正则表达式进行查找替换 推荐
- PHP - Manual手册 - CLXI. String 字符串处理函数 - strpos查找一个字符串第一次出现的位置
- 算法动态演示http://cst.hit.edu.cn/datastructure/teachassist/suanfadongtaiyanshi.php