什么是二分查找?
二分搜索是一种搜索算法,用于有效地查找排序数组(或列表)中目标值的位置。它的工作原理是重复将搜索范围一分为二,并将中间元素与目标值进行比较。
二分查找算法遵循以下步骤:
从整个排序数组开始。
将左指针设置为数组的第一个元素,将右指针设置为最后一个元素。
计算中间索引作为左右指针的平均值(整数除法)。
将中间索引处的值与目标值进行比较。
如果中间值等于目标值,则搜索成功,算法返回索引。
如果目标值大于中间值,则通过将左指针更新为 mid + 1 来消除搜索范围的左半部分。
如果目标值小于中间值,则通过将右指针更新为 mid - 1 来消除搜索范围的右半部分。
重复步骤3到7,直到找到目标值或搜索范围为空(左指针大于右指针)。
如果搜索范围为空并且未找到目标值,则算法得出结论:目标值不存在于数组中并返回 -1 或适当的指示。
二分查找是一种非常高效的算法,时间复杂度为 O(log n),其中 n 是数组中元素的数量。它对于大型排序数组特别有效,因为它通过在每一步将搜索范围一分为二来快速缩小搜索范围,即使有大量元素也可以快速搜索。
二分查找的 PHP 程序
方法 1 - 使用迭代
示例
<?php function binarySearch($arr, $target) { $left = 0; $right = count($arr) - 1; while ($left <= $right) { $mid = floor(($left + $right) / 2); // Check if the target value is found at the middle index if ($arr[$mid] === $target) { return $mid; } // If the target is greater, ignore the left half if ($arr[$mid] < $target) { $left = $mid + 1; } // If the target is smaller, ignore the right half else { $right = $mid - 1; } } // Target value not found in the array return -1; } // Example usage 1 $sortedArray = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]; $targetValue = 91; $resultIndex = binarySearch($sortedArray, $targetValue); if ($resultIndex === -1) { echo "Target value not found in the array.<br>"; } else { echo "Target value found at index $resultIndex.<br>"; } // Example usage 2 $targetValue = 42; $resultIndex = binarySearch($sortedArray, $targetValue); if ($resultIndex === -1) { echo "Target value not found in the array."; } else { echo "Target value found at index $resultIndex."; } ?>
输出
Target value found at index 9. Target value not found in the array.
方法 2 - 使用递归
示例
<?php function binarySearchRecursive($arr, $target, $left, $right) { if ($left > $right) { // Target value not found in the array return -1; } $mid = floor(($left + $right) / 2); // Check if the target value is found at the middle index if ($arr[$mid] === $target) { return $mid; } // If the target is greater, search the right half if ($arr[$mid] < $target) { return binarySearchRecursive($arr, $target, $mid + 1, $right); } // If the target is smaller, search the left half return binarySearchRecursive($arr, $target, $left, $mid - 1); } // Wrapper function for the recursive binary search function binarySearch($arr, $target) { $left = 0; $right = count($arr) - 1; return binarySearchRecursive($arr, $target, $left, $right); } // Example usage $sortedArray = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]; $targetValue = 16; $resultIndex = binarySearch($sortedArray, $targetValue); if ($resultIndex === -1) { echo "Target value not found in the array."; } else { echo "Target value found at index $resultIndex."; } ?>
输出
Target value found at index 4.
结论
总之,二分搜索是一种强大的算法,可以在排序数组中有效地查找目标值。它提供了两种常见的实现:迭代和递归。迭代方法使用 while 循环重复将搜索范围一分为二,直到找到目标值或范围变空。它具有简单的实现方式,非常适合大多数场景。另一方面,递归方法采用递归函数来执行二分搜索。它遵循与迭代方法相同的逻辑,但使用函数调用而不是循环。递归二分搜索提供了更简洁的实现,但由于函数调用堆栈操作可能具有稍高的开销。总的来说,这两种方法都提供了执行二分搜索操作的高效可靠的方法。
以上是在PHP中进行二分查找的详细内容。更多信息请关注PHP中文网其他相关文章!

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP日志记录对于监视和调试Web应用程序以及捕获关键事件,错误和运行时行为至关重要。它为系统性能提供了宝贵的见解,有助于识别问题并支持更快的故障排除

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

禅工作室 13.0.1
功能强大的PHP集成开发环境