Can PHP write algorithms?

藏色散人
Release: 2023-02-27 18:46:01
Original
3337 people have browsed it

Can PHP write algorithms?

Can I write algorithms in php?

Of course php can write algorithms.

For example, PHP implements the bubble sort algorithm to sort a set of numbers from small to large.

Idea: From small to large, the first round ranks the smallest, the second round ranks the second smallest, the third round ranks the third smallest, and so on...

<?php
$arr = array(3, 2, 1);
$n = count($arr);
//每循环一次,就跑一趟后面的排序
for($j=0; $j<$n-1; $j++) {
//对后面没排好的,循环查找出最大(最小)的,进行一趟排序
    for($i=$j; $i<$n-1; $i++) {
        if($arr[$j] > $arr[$i+1]) {
            $t = $arr[$j];
            $arr[$j] = $arr[$i+1];
            $arr[$i+1] = $t;
        }
    }
}
print_r($arr);
Copy after login

More PHP For relevant knowledge, please visit PHP中文网!

The above is the detailed content of Can PHP write algorithms?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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!