欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

排序 查找

程序员文章站 2022-06-04 18:49:01
$array[$j]){ $temp = $array[$i]; $array[$i] = $array[$j]; $array[$j] = $temp; } } } return $ar... ......
<?php
    /*
     *冒泡排序
     */
    function maopao($array){
        for($i =0;$i < count($array);$i++){
            for($j = $i+1;$j < count($array);$j++){
                if($array[$i] > $array[$j]){
                    $temp = $array[$i];
                    $array[$i] = $array[$j];
                    $array[$j] = $temp;
                }
            }
        }
        return $array;
    }

    /*
     * 二分查找
     */

    function erfen($array,$search,$low = 0,$hight = 100)
    {
        $midpostion = floor(($low + $hight)/2);
        $middata = $array[$midpostion];
        if($middata == $search)
        {
            return $midpostion;
        }
        if($search < $middata)
        {
            $hight = $midpostion;
            if($hight == 0)
            {
                return false;
            }
            return erfen($array,$search,$low,$hight);
        }else{
            $low = $midpostion + 1;
            if($low > $hight){
                return false;
            }
            return erfen($array,$search,$low,$hight);
        }
    }

    /*
     * 100+99+98+.......1;
     */

    function leijia($n)
    {
        if($n == 1){
            return $n;
        }
        return $n + leijia($n-1);
    }


    $a= array(9,4,6,8,2,4,5,1);
    $b= maopao($a);

    $c = array(1,2,3,4,5,6,7,8,9);
    $k = 5;
    $d = erfen($c,$k,0,8);

    $sum = leijia(100);
    echo $sum;