PHP兑现的几种常见算法

WBOY
Release: 2016-06-13 13:01:32
Original
1091 people have browsed it

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 

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!