Home Backend Development PHP Problem Do you need to learn algorithms in PHP?

Do you need to learn algorithms in PHP?

Oct 19, 2019 am 09:50 AM
php algorithm

Do you need to learn algorithms in PHP?

Does PHP need to learn algorithms? Basically, they are all about functions, and rarely come into contact with data structures and algorithms, but we need to know that program=data structure algorithm

, which shows how important algorithms are to programs.

If you want to go to the advanced level, you must know algorithms and data structures, but you cannot use PHP to implement them, because PHP's operating efficiency is too low, and a large number of loops are generally required to complete algorithm-level things. Therefore, the standard way to implement mathematical problems and algorithms in PHP should be to make them C extensions, which means that the algorithms must be implemented in C, and you must learn how to develop PHP C extensions.

Basic algorithm:

Bubble sort

//逐行对比,满足条件则交换
function bubbleSort($arrData,$sort = 'desc')
{
    if(empty($arrData)) return $arrData;
    //默认有序
    $isSorted = true;
    $nCount = count($arrData);
    for($i = 0; $i < $nCount; $i++) {
        //对比次数随着循环逐渐减少,因为后面的数据已经处理为有序
        for($j = 0; $j < ($nCount - $i - 1); $j++) {
            //执行判断
            $isChange = $sort == &#39;desc&#39; ? $arrData[$j] < $arrData[$j+1] : $arrData[$j] > $arrData[$j+1];
            if($isChange) {
                //首次对比,判断是否有序
                $isSorted = false;
                $temp = $arrData[$j];
                $arrData[$j] = $arrData[$j+1];
                $arrData[$j+1] = $temp;
            }
        }
        if($isSorted) break;
    }
    return $arrData;
}
Copy after login
Quick sort

//选取一个标准,和其他数据对比后将数据分为两批,递归执行后合并
function quickSort(&$arr, $sort = &#39;asc&#39;){
    //检查数据,多于一个数据才执行
    $nCount = count($arr);
    if($nCount > 1) {
        //选取标准(第一个数据)
        $nStandard = $arr[0];
        $arrLeftData = [];
        $arrRightData = [];
        //遍历,注意这里从1开始比较
        for($i = 1; $i < $nCount; $i++) {
            if($sort == &#39;desc&#39;) {
                $arr[$i] > $nStandard ? $arrLeftData[] = $arr[$i] : $arrRightData[] = $arr[$i];
            } else {
                $arr[$i] > $nStandard ? $arrRightData[] = $arr[$i] : $arrLeftData[] = $arr[$i];
            }
        }
        $arr = array_merge($this->quickSort($arrLeftData, $sort), array($nStandard), $this->quickSort($arrRightData, $sort));
    }
    return $arr;
}
Copy after login
Binary search

//假设数据是按升序排序的,对于给定值x,从序列的中间位置开始比较,如果当前位置值等于x,则查找成功;
//若x小于当前位置值,则在数列的前半段中查找;若x大于当前位置值则在数列的后半段中继续查找,直到找到为止
function binSearch($toSearch,$arr)
{
    //确定当前的检索范围
    $nCount = count($arr);
    //低位键,初始为0
    $nLowNum = 0;
    //高位键,初始为末尾 
    $nHighNum = $nCount - 1;
    while($nLowNum <= $nHighNum) {
        //选定大概中间键
        $nMiddleNum = intval(($nHighNum + $nLowNum)/2);
        if($arr[$nMiddleNum] > $toSearch) {
            //比检索值大
            $nHighNum = $nMiddleNum - 1;
        } elseif ($arr[$nMiddleNum] < $toSearch) {
            //比检索值小
            $nLowNum = $nMiddleNum + 1;
        } else {
            return $nMiddleNum;
        }
    }
    return false;
}
Copy after login
Sequential search

function seqSearch($arr,$toSearch)
{
    $nCount = count($arr);
    for ($i=0; $i < $nCount; $i++) {
        if ($arr[$i] == $toSearch) {
            return $i;
        }
    }
    return -1;
}
Copy after login
Select sort

//在第一次循环中,假设第一个数是最小的;然后跟第二个数比较,一直比到最后,找出最小值,然后把最小值跟第一个数的位置互换;
//再进行下一次循环,找出最小值跟第二个位置的数互换;一直循环数组的个数减去1次;数组就成了有序的了 
function selectSort($arr)
{
    $nCount = count($arr);
    //遍历取得需要排序的数
    for($i = 0; $i < $nCount; $i++) {
        //选择需要比较的数,从$i开始到结束
        for($j = $i + 1; $j < $nCount; $j++) {
            //比较
            if($arr[$j] < $arr[$i]) {
                $temp = $arr[$i];
                $arr[$i] = $arr[$j];
                $arr[$j] = $temp;
            }
        }
    }
    return $arr;  
}
Copy after login
For more PHP related knowledge, please visit PHP Chinese website

!

The above is the detailed content of Do you need to learn algorithms in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles