Home > php教程 > php手册 > body text

在php中用for循环对数组进行冒泡排序并打印

WBOY
Release: 2018-10-27 14:19:37
forward
1973 people have browsed it


这篇文章主要介绍了在php中用for循环对数组进行冒泡排序并打印 ,有一定的参考价值,感兴趣的朋友可以看看,希望可以帮助到你!

<!DOCTYPE html>
<html>
<body>
<h1>原来是PHP啊!</h1>
<?php
//冒泡排序并打印排序后的数组
$a=array(2,1,4,3,5,6);
$length=count($a);
for($i=0;$i<$length;$i++){
for($j=0;$j<$length-1-$i;$j++){
if($a[$j+1]<$a[$j]){
$b=$a[$j];
$a[$j]=$a[$j+1];
$a[$j+1]=$b;
}
}
echo $a[$i]," ";
}
?>  
</body>
</html>
Copy after login

结果为1 2 3 4 5 6

当然,也可以直接使用函数sort()或者rsort()直接进行排序。

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程 

Related labels:
source:csdn.net
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template