Home  >  Article  >  Backend Development  >  Bubble sort algorithm Detailed interpretation of PHP bubble sort algorithm code

Bubble sort algorithm Detailed interpretation of PHP bubble sort algorithm code

WBOY
WBOYOriginal
2016-07-29 08:46:061376browse

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++){
//Why $n-1 is because the array is calculated from 0
//Next is the first time 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 ];
//This is the time to exchange positions
$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 begin
}
}
}
?>

The above has introduced the bubble sort algorithm and a detailed interpretation of the PHP bubble sort algorithm code, including the content of the bubble sort algorithm. I hope it will be helpful to friends who are interested in PHP tutorials.

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