Home  >  Article  >  Backend Development  >  Can PHP write algorithms?

Can PHP write algorithms?

藏色散人
藏色散人Original
2019-10-26 09:47:423353browse

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);

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!

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