就是简单描述一下顺序查找和二分查找
程序员文章站
2022-05-12 08:25:58
...
array as $k => $v) { if($v == $val) { echo '顺序查找成功!'; exit(0); } } echo '顺序查找失败!'; } /** * 二分查找法 * @param $val 要查找的值 */ public function bin_search($val) { sort($this->array); $min = 0; $max = count($this->array); for ($i = $min; $i < $max; $i++) { $mid = ceil(($min + $max) / 2); if($val == $this->array[$mid]) { echo '二分查找成功!'; exit(0); } else if($val < $this->array[$mid]) { $max = $mid; } else if($val > $this->array[$mid]) { $min = $mid; } } echo '二分查找失败!'; } }
上一篇: 阻止表单提交怎么用onsubmit呢?
下一篇: 简单正则