Home >Backend Development >PHP Tutorial >PHP冒泡排序算法代码详细解读_php技巧

PHP冒泡排序算法代码详细解读_php技巧

WBOY
WBOYOriginal
2016-05-17 09:17:131744browse
复制代码 代码如下:

$arr = array(345,4,17,6,52,16,58,69,32,8,234);
$n = count($arr);
for($i=1;$i//其中的为什么$n-1是因为数组是从0开始计算的
//接下来是第一次内循环
for($j=$n-1;$j>=$i;$j--)
{
//如果$arr[10]//temp = $arr[9];
if($arr[$j]//$temp 暂时先把小的值放起来
$temp = $arr[$j-1];
//这个时候开始要交换位置了
$arr[$j-1] = $arr[$j];
//$arr[9] = $arr[10]的值
$arr[$j] = $temp;
//$arry[10]的值等于$arr[9]的值
//这个时候就要开始交换位置了
}
}
}
?>
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