The complete code for implementing bubble sorting and binary search in PHP

不言
Release: 2023-04-03 19:34:01
Original
1656 people have browsed it

This article brings you the complete code for implementing bubble sorting and binary search in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. helped.

<?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;
Copy after login

Related recommendations:

Commonly used sorting and search algorithms in PHP, PHP sorting algorithm

PHP Bubble Sort Two-point Search Sequential search Detailed explanation of two-dimensional array sorting algorithm function

PHP common algorithms [bubble sort, quick sort, insertion sort, rounding sort, binary search, .]

The above is the detailed content of The complete code for implementing bubble sorting and binary search in PHP. For more information, please follow other related articles on the PHP Chinese website!

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