Home > Backend Development > PHP Tutorial > Detailed interpretation of PHP bubble sort algorithm code_PHP tutorial

Detailed interpretation of PHP bubble sort algorithm code_PHP tutorial

WBOY
Release: 2016-07-21 15:26:13
Original
881 people have browsed it

Copy code The code is as follows:

$arr = array(345,4,17,6,52 ,16,58,69,32,8,234);
$n = count($arr);
for($i=1;$i<$n;$i++){
//where Why $n-1 is because the array is calculated from 0
//The next is the first inner loop
for($j=$n-1;$j>=$i;$j --)
{
//If $arr[10]<$arr[9];
//temp = $arr[9];
if($arr[$j] <$arr[$j-1]){
//$temp put the small values ​​together for now
$temp = $arr[$j-1];
//Start at this time The positions are about to be swapped
$arr[$j-1] = $arr[$j];
//$arr[9] = the value of $arr[10]
$arr[$j] = $temp;
//The value of $arry[10] is equal to the value of $arr[9]
//At this time, the positions will start to be exchanged
}
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323999.htmlTechArticleCopy the code as follows: ?php $arr = array(345,4,17,6,52,16, 58,69,32,8,234); $n = count($arr); for($i=1;$i$n;$i++){ //Why $n-1 is because the array starts from 0 Calculated...
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