当前位置:首页 > PHP

php 实现算法

2026-01-30 02:58:23PHP

PHP 实现常见算法的方法

PHP 作为一门服务器端脚本语言,可以实现多种算法。以下是一些常见算法的 PHP 实现示例。

排序算法

冒泡排序

function bubbleSort($array) {
    $length = count($array);
    for ($i = 0; $i < $length - 1; $i++) {
        for ($j = 0; $j < $length - $i - 1; $j++) {
            if ($array[$j] > $array[$j + 1]) {
                $temp = $array[$j];
                $array[$j] = $array[$j + 1];
                $array[$j + 1] = $temp;
            }
        }
    }
    return $array;
}

快速排序

function quickSort($array) {
    if (count($array) <= 1) {
        return $array;
    }
    $pivot = $array[0];
    $left = $right = [];
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i] < $pivot) {
            $left[] = $array[$i];
        } else {
            $right[] = $array[$i];
        }
    }
    return array_merge(quickSort($left), [$pivot], quickSort($right));
}

搜索算法

二分查找

php 实现算法

function binarySearch($array, $target) {
    $low = 0;
    $high = count($array) - 1;
    while ($low <= $high) {
        $mid = floor(($low + $high) / 2);
        if ($array[$mid] == $target) {
            return $mid;
        }
        if ($array[$mid] < $target) {
            $low = $mid + 1;
        } else {
            $high = $mid - 1;
        }
    }
    return -1;
}

递归算法

斐波那契数列

function fibonacci($n) {
    if ($n == 0) {
        return 0;
    }
    if ($n == 1) {
        return 1;
    }
    return fibonacci($n - 1) + fibonacci($n - 2);
}

动态规划

最长公共子序列

function lcs($X, $Y) {
    $m = strlen($X);
    $n = strlen($Y);
    $L = array_fill(0, $m + 1, array_fill(0, $n + 1, 0));
    for ($i = 0; $i <= $m; $i++) {
        for ($j = 0; $j <= $n; $j++) {
            if ($i == 0 || $j == 0) {
                $L[$i][$j] = 0;
            } elseif ($X[$i - 1] == $Y[$j - 1]) {
                $L[$i][$j] = $L[$i - 1][$j - 1] + 1;
            } else {
                $L[$i][$j] = max($L[$i - 1][$j], $L[$i][$j - 1]);
            }
        }
    }
    return $L[$m][$n];
}

加密算法

MD5 哈希

php 实现算法

$hash = md5('your_string');

SHA-256 哈希

$hash = hash('sha256', 'your_string');

数据结构实现

class Stack {
    private $stack = [];

    public function push($item) {
        array_push($this->stack, $item);
    }

    public function pop() {
        if ($this->isEmpty()) {
            return null;
        }
        return array_pop($this->stack);
    }

    public function isEmpty() {
        return empty($this->stack);
    }
}

队列

class Queue {
    private $queue = [];

    public function enqueue($item) {
        array_push($this->queue, $item);
    }

    public function dequeue() {
        if ($this->isEmpty()) {
            return null;
        }
        return array_shift($this->queue);
    }

    public function isEmpty() {
        return empty($this->queue);
    }
}

以上示例展示了 PHP 实现常见算法的方式,可以根据具体需求进行调整和优化。

标签: 算法php
分享给朋友:

相关文章

php实现定时任务

php实现定时任务

PHP 实现定时任务的几种方法 在 PHP 中实现定时任务可以通过多种方式完成,具体选择取决于项目需求和服务器环境。以下是常见的实现方法: 使用 Cron 任务 Cron 是 Linux 系统中常用…

php实现mvc

php实现mvc

PHP 实现 MVC 架构 MVC(Model-View-Controller)是一种常用的软件设计模式,将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller…

php实现注册登陆

php实现注册登陆

PHP 注册功能实现 创建数据库表结构(MySQL示例): CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, usern…

php实现进度条

php实现进度条

PHP 实现进度条的方法 使用 HTML 和 CSS 结合 PHP 实现进度条 在 PHP 中,进度条通常通过 HTML 和 CSS 实现,PHP 负责计算进度并更新前端显示。 <div st…

php实现验证码的识别

php实现验证码的识别

验证码识别的基本方法 验证码识别通常分为手动实现和第三方工具两种方式。手动实现需要图像处理和机器学习知识,而第三方工具则更快捷。 使用Tesseract OCR识别验证码 Tesseract是一个开…

php 实现排名

php 实现排名

PHP 实现排名的方法 在 PHP 中实现排名功能通常涉及对数据进行排序、计算排名以及输出结果。以下是几种常见的实现方式: 基本数组排序排名 $scores = [85, 92, 78, 95, 8…