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

Do you need to learn algorithms in PHP?

angryTom
angryTomOriginal
2019-10-19 09:50:052812browse

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;
}
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;
}
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;
}
Sequential search

function seqSearch($arr,$toSearch)
{
    $nCount = count($arr);
    for ($i=0; $i < $nCount; $i++) {
        if ($arr[$i] == $toSearch) {
            return $i;
        }
    }
    return -1;
}
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;  
}
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:
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