PHP实现折半查询算法

PHP实现折半查询算法,自己写代码,可能不规范。

什么是折半查询算法?具体文字描述自己百度。直接上代码:

<?php
header("Content-type: text/html; charset=utf-8"); 

/* 折半查询算法--不用递归 */
function qSort($data = array(), $x = 0){
	$startIndex = 0;                // 开始索引
	$endIndex = count($data) - 1;   // 结束索引
	$index = 0;
	$number = 0;                    // 计数器
	do{
		if($endIndex > $startIndex){
			$searchIndex = ceil(($endIndex - $startIndex) / 2);
		}else if($endIndex == $startIndex){
			$searchIndex = $endIndex;
		}else{
			$index = -1;
			break;
		}
		$searchIndex += ($startIndex - 1);

		echo '检索范围:'.$startIndex.' ~ '.$endIndex.'<br>检索位置:'.$searchIndex.'检索值为:'.$data[$searchIndex];
		echo '<br>=======================<br><br>';

		if($data[$searchIndex] == $x){
			$index = $searchIndex;
			break;
		}else if($x > $data[$searchIndex]){
			$startIndex = $searchIndex + 1;
		}else{
			$endIndex = $searchIndex - 1;
		}

		$number++;
	}while($number < count($data));
	return $index;
}

/* 折半查询算法--使用递归 */
function sSort($data, $x, $startIndex, $endIndex){
	if($endIndex > $startIndex){
		$searchIndex = ceil(($endIndex - $startIndex) / 2);
	}else if($endIndex == $startIndex){
		$searchIndex = $endIndex;
	}else{
		return -1;
	}

	$searchIndex += ($startIndex - 1);

	echo '检索范围:'.$startIndex.' ~ '.$endIndex.'<br>检索位置:'.$searchIndex.'检索值为:'.$data[$searchIndex];
	echo '<br>=======================<br><br>';

	if($data[$searchIndex] == $x){
		return $searchIndex;
	}else if($x > $data[$searchIndex]){
		$startIndex = $searchIndex + 1;
		return sSort($data, $x, $startIndex, $endIndex);
	}else{
		$endIndex = $searchIndex - 1;
		return sSort($data, $x, $startIndex, $endIndex);
	}
}

$data = array(1, 3, 4, 6, 9, 11, 12, 13, 15, 20, 21, 25, 33, 34, 35, 39, 41, 44);

$index = qSort($data, 11);                       // 不用递归的排序方法
$index = sSort($data, 11, 0, count($data) - 1);  // 使用递归的排序方法
echo '结果:'.$index;




### 顺序查找算法 顺序查找是一种基本的查找方法,适用于无序列表。该算法通过遍历整个数组来寻找目标值。 ```php function sequentialSearch($arr, $target) { foreach ($arr as $index => $value) { if ($value === $target) { return $index; } } return -1; // 如果未找到则返回-1表示不存在此元素 } ``` 这段代码展示了如何在一个给定数组`$arr`中逐个比较每个元素与目标值`$target`是否相等[^2]。一旦发现匹配项即刻返回其索引位置;反之,在完成全部扫描后仍未遇到相同数值,则表明目标不在集合内,最终输出-1作为指示标志。 ### 折半查找算法 折半查找也称为二分查找,它依赖于已排序的数据集,并利用中间点逐步缩小搜索范围直至定位到所需元素或确认其缺失状态。为了应用这种高效的检索策略,前提条件是待查序列需按升序排列且采用连续内存分配方式存储各节点信息[^3]。 #### 非递归版本: ```php function binarySearchNonRecursive($arr, $target) { $low = 0; $high = count($arr) - 1; while ($low <= $high) { $mid = floor(($low + $high) / 2); if ($arr[$mid] < $target) { $low = $mid + 1; } elseif ($arr[$mid] > $target) { $high = $mid - 1; } else { return $mid; // 找到了就返回对应的下标 } } return -1; // 若找不到对应的目标数则返回-1 } ``` 上述函数实现了基于循环控制结构而非调用自身的迭代过程来进行二叉树式的区间分割操作,从而有效地减少了不必要的重复计算开销[^1]. #### 递归版本: ```php function binarySearchRecursive($arr, $target, $low, $high) { if ($low > $high) { return -1; // 基本情况:当低限超过高限时停止搜索并报告失败 } $mid = floor(($low + $high) / 2); if ($arr[$mid] == $target) { return $mid; // 成功找到了目标的位置 } elseif ($arr[$mid] > $target) { return binarySearchRecursive($arr, $target, $low, $mid - 1); // 向左子区继续探索 } else { return binarySearchRecursive($arr, $target, $mid + 1, $high); // 转向右子区深入考察 } } // 使用示例: // 初始化参数 $startIndex = 0; $endIndex = count($sortedArray) - 1; $result = binarySearchRecursive($sortedArray, $searchValue, $startIndex, $endIndex); ``` 这里提供了一个更直观理解逻辑流程的方式——借助函数自我调用来模拟人类思维模式下的决策路径选择机制,每次都将问题规模减半直到触及边界状况为止.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值