search
Homephp教程php手册PHP冒泡法排序与二分法查找实例

冒泡法排序与二分法查找排序算法是我们在初中时就学过的,下面我来介绍在PHP冒泡法排序与二分法查 找实例,各位同学不防进入参考.

冒泡法排序,随便给出一个乱序数组

$arr = array(0,2,10,9,19,23,89,112,321,234);

统计数组:$num = count($arr);

冒泡倒序排列:

<?php
for ($i = 0; $i < $num - 1; $i++) {
    for ($m = 0; $m < $num - 1; $m++) {
        if ($arr[$m] < $arr[$m + 1]) {
            $temp = $arr[$m];
            $arr[$m] = $arr[$m + 1];
            $arr[$m + 1] = $temp;
        }
        // echo $arr[$m].&#39;<br>&#39;;
        
    }
}
//输出排序后的结果
var_dump($arr);
//冒泡顺序排列
for ($x = 0; $x < $num - 1; $x++) {
    for ($y = 0; $y < $num - 1; $y++) {
        if ($arr[$y] > $arr[$y + 1]) {
            $temp = $arr[$y];
            $arr[$y] = $arr[$y + 1];
            $arr[$y + 1] = $temp;
        }
    }
}
//输出排序后的结果
var_dump($arr);
//二分法查找
function dichotomy($array, $k, $low = 0, $high = 0) {
    if (count($array) != 0 && $high == 0) {
        $high = count($array);
    }
    if ($low <= $high) {
        $mid = intval(($low + $high) / 2);
        if ($array[$mid] == $k) {
            return $mid;
        } elseif ($k < $array[$mid]) {
            return dichotomy($array, $k, $low = 0, $mid - 1);
        } else {
            return dichotomy($array, $k, $mid + 1, $high);
        }
    } else {
        return false;
    }
}
//输出查找结果
echo dichotomy($arr, 23);
?>

今天简单的研究了一下最常用的冒泡法排序与二分法查找,写了一个简单的案例,加强自己对php的学习,也希望对今后php学习者能提供一点点的帮助。

本文地址:

转载随意,但请附上文章地址:-)

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.