Home  >  Article  >  Backend Development  >  PHP兑现的几种常见算法

PHP兑现的几种常见算法

WBOY
WBOYOriginal
2016-06-13 13:01:321101browse

PHP实现的几种常见算法
/冒泡排序(数组排序) 
function bubble_sort($array) 

        $count = count($array); 
        if ($count 
        for($i=0; $i                 for($j=$count-1; $j>$i; $j--){ 
                        if ($array[$j]                                  $tmp = $array[$j]; 
                                $array[$j] = $array[$j-1]; 
                                $array[$j-1] = $tmp; 
                        } 
                } 
        } 
        return $array; 
}

//快速排序(数组排序) 
function quick_sort($array) { 
        if (count($array) 
        $key = $array[0]; 
        $left_arr = array(); 
        $right_arr = array();

        for ($i=1; $i                 if ($array[$i]                          $left_arr[] = $array[$i]; 
                else 
                        $right_arr[] = $array[$i]; 
        }

        $left_arr = quick_sort($left_arr); 
        $right_arr = quick_sort($right_arr);

        return array_merge($left_arr, array($key), $right_arr); 
}

//二分查找(数组里查找某个元素) 
function bin_sch($array, $low, $high, $k){ 
    if ($low          $mid = intval(($low+$high)/2); 
        if ($array[$mid] == $k){ 
            return $mid; 
        }elseif ($k 

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