PHP

搜索算法

1. 顺序搜索

// 顺序搜索算法
function sequentialSearch($arr,$value){ 
    $len = count($arr); 
    for($i=0;$i<$len;$i++){ 
        if($arr[$i] == $value){ 
            return $i; 
        } 
    } 
    return -1; 
} 

2. 二分搜索算法

// 二分搜索算法
function binarySearch($arr,$value){ 
    $low = 0; 
    $high = count($arr)-1; 
    while($low <= $high){ 
        $mid = intval(($low+$high)/2); 
        if($arr[$mid] == $value){ 
            return $mid; 
        }elseif($arr[$mid] > $value){ 
            $high = $mid-1; 
        }else{ 
            $low = $mid+1; 
        } 
    } 
    return -1; 
} 

3. 插值搜索算法

// 插值搜索算法
function interpolationSearch($arr,$value){ 
    $low = 0; 
    $high = count($arr)-1; 
    while($low<=$high){ 
        $mid = $low + intval(($high-$low)*($value-$arr[$low])/($arr[$high]-$arr[$low])); 
        if($arr[$mid] == $value){ 
            return $mid; 
        }elseif($arr[$mid] > $value){ 
            $high = $mid-1; 
        }else{ 
            $low = $mid+1; 
        } 
    } 
    return -1; 
} 

4. 斐波那契搜索算法

// 斐波那契搜索算法
function fibonacciSearch($arr,$value){ 
    $fibonacci_num = array(0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946); 
    $k = 0; 
    while($fibonacci_num[$k]<count($arr)){ 
        $k++; 
    } 
    $offset = -1; 
    while($fibonacci_num[$k]>1){ 
        $i = min($offset+$fibonacci_num[$k-1],count($arr)-1); 
        if($arr[$i] < $value){ 
            $k = $k-1; 
            $offset = $i; 
        }elseif($arr[$i] > $value){ 
            $k = $k-2; 
        }else{ 
            return $i; 
        } 
    } 
    if($fibonacci_num[$k-1] && $arr[$offset+1]==$value){ 
        return $offset+1; 
    } 
    return -1; 
} 

5. 深度优先搜索算法

function dfs($node, $visited) {
    $visited[] = $node;  // 将节点加入已访问列表

    // 对当前节点的所有邻居节点进行深度优先搜索
    foreach ($node->neighbors as $neighbor) {
        if (!in_array($neighbor, $visited)) {
            dfs($neighbor, $visited);
        }
    }
}

6. 广度优先搜索算法

function bfs($node, $visited) {
    $queue = [$node];  // 将当前节点加入队列

    while (!empty($queue)) {
        $node = array_shift($queue);  // 从队列中取出一个节点
        $visited[] = $node;  // 将节点加入已访问列表

        // 将当前节点的所有邻居节点加入队列
        foreach ($node->neighbors as $neighbor) {
            if (!in_array($neighbor, $visited)) {
                $queue[] = $neighbor;
            }
        }
    }
}

7. 启发式搜索

// 启发式搜索
function heuristicSearch($node, $visited, $heuristic) {
    $queue = [$node];  // 将当前节点加入队列

    while (!empty($queue)) {
        // 按照启发式函数的值对队列中的节点进行排序
        usort($queue, function($a, $b) use ($heuristic) {
            return $heuristic($a) - $heuristic($b);
        });

        $node = array_shift($queue);  // 从队列中取出一个节点
        $visited[] = $node;  // 将节点加入已访问列表

        // 将当前节点的所有邻居节点加入队列
        foreach ($node->neighbors as $neighbor) {
            if (!in_array($neighbor, $visited)) {
                $queue[] = $neighbor;
            }
        }
    }
}

8. 遗传算法

// 初始化种群
$population = [];
for ($i = 0; $i < POPULATION_SIZE; $i++) {
    $individual = generateRandomIndividual();
    $population[] = $individual;
}

// 循环迭代
while (true) {
    // 计算适应度
    $fitnesses = [];
    foreach ($population as $individual) {
        $fitnesses[] = calculateFitness($individual);
    }

    // 选择
    $selected = select($population, $fitnesses);

    // 交叉
    $crossed = [];
    for ($i = 0; $i < count($selected); $i += 2) {
        $parent1 = $selected[$i];
        $parent2 = $selected[$i + 1];

        $children = cross($parent1, $parent2);
        $crossed[] = $children[0];
        $crossed[] = $children[1];
    }

    // 变异
    $mutated = [];
    foreach ($crossed as $individual) {
        $mutated[] = mutate($individual);
    }

    // 更新种群
    $population = $mutated;

    // 停止条件
    if (isStopCondition()) {
        break;
    }
}

9. 分块搜索

// 分块搜索
function blockSearch($arr, $value){
    $blockSize = sqrt(count($arr));
    $blockIndex = intval($value / $blockSize);
    $low = $blockIndex * $blockSize;
    $high = ($blockIndex + 1) * $blockSize - 1;
    while ($low <= $high) {
        if ($arr[$low] == $value) {
            return $low;
        } elseif ($arr[$high] == $value) {
            return $high;
        } else {
            $low++;
            $high--;
        }
    }
    return -1;
}

10. 哈希搜索

// 哈希搜索
function hashSearch($arr, $value){
    $hashTable = array_fill(0, count($arr), -1);
    for ($i = 0; $i < count($arr); $i++) {
        $hashTable[$arr[$i]] = $i;
    }
    if (isset($hashTable[$value])) {
        return $hashTable[$value];
    } else {
        return -1;
    }
}


评论 0

还没有评论,来说两句吧。

验证码

登录后即可评论。评论需要填写验证码。