算法:二分查找,使用循环或递归;使用php、python;

二分搜索是一种在有序数组中查找某一特定元素的搜索算法。搜索过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束;如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。如果在某一步骤数组为空,则代表找不到。这种搜索算法每一次比较都使搜索范围缩小一半。

//php循环
function binarySearch($array, $target) {  
    $low = 0;  
    $high = count($array) - 1;  
    while ($low <= $high) {  
        $mid = intval(($low + $high) / 2);  
        if ($array[$mid] == $target) {  
            return $mid; // 目标值找到,返回其索引  
        }  
        if ($array[$mid] < $target) {  
            $low = $mid + 1; // 在右半部分查找  
        } else {  
            $high = $mid - 1; // 在左半部分查找  
        }  
    }  
    return -1; // 目标值不存在于数组中  
}  
// 示例数组,必须是有序的  
$array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19];  
$target = 13;  
$result = binarySearch($array, $target);  
if ($result != -1) {  
    echo "元素在数组中的索引为: " . $result;  
} else {  
    echo "元素不在数组中";  
}
//php递归
function binarySearchRecursive($array, $target, $low = 0, $high = null) {  
    if ($high === null) {  
        $high = count($array) - 1;  
    }  
    if ($low > $high) {  
        return -1; // 目标值不存在于数组中  
    }  
    $mid = intval(($low + $high) / 2);  
    if ($array[$mid] == $target) {  
        return $mid; // 目标值找到,返回其索引  
    } elseif ($array[$mid] < $target) {  
        return binarySearchRecursive($array, $target, $mid + 1, $high); // 在右半部分查找  
    } else {  
        return binarySearchRecursive($array, $target, $low, $mid - 1); // 在左半部分查找  
    }  
}    
// 示例数组,必须是有序的  
$array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19];  
$target = 13;  
$result = binarySearchRecursive($array, $target);   
if ($result != -1) {  
    echo "元素在数组中的索引为: " . $result;  
} else {  
    echo "元素不在数组中";  
}
#python循环
def binarySearch(array,target):
	low = 0
	high = len(array)-1
	
	while low<=high:
		mid = (low+high)//2
		if target==array[mid]:
			return mid #目标值找到,返回其索引
		if array[mid]<target:
			low = mid+1 #在右半部分寻找
		else:
			high = mid-1 #在左半部分寻找
	else:
		return -1;

array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
target = 13
result = binarySearch(array,target)
if result!=-1:
	print("元素在数组中的索引为:",result)
else:
	print("元素不在数组中")
#python递归
def binarySearchRecursive(array,target,low=0,high=None):
	if high is None:
		high = len(array)-1
	
	if low>high:
		return -1; # 目标值不存在于数组中  
	
	mid = (low+high)//2
	if target==array[mid]:
		return mid #目标值找到,返回其索引
	if array[mid]<target:
		return binarySearchRecursive(array,target,mid+1,high)#在右半部分寻找
	else:
		return binarySearchRecursive(array,target,low,mid-1)#在左半部分寻找

array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
target = 13
result = binarySearchRecursive(array,target)
if result!=-1:
	print("元素在数组中的索引为:",result)
else:
	print("元素不在数组中")

在这个递归版本的二分查找中,我们同样传递了四个参数:array(待搜索的数组)、target(目标值)、low(搜索范围的起始索引)和high(搜索范围的结束索引)。如果 high 没有被显式传递,则默认为数组的最后一个元素的索引。

函数首先检查 low 是否大于 high,如果是,则搜索范围已经为空,返回 -1 表示目标值不存在于数组中。然后,计算中间元素的索引 mid,并检查它是否等于目标值。如果等于,返回 mid。否则,根据中间元素与目标值的大小关系,递归地在数组的左半部分或右半部分继续搜索。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值