- //Array element values are sorted from small to large
- $arr=array(1,42,0,3,15,7,19,26);
- //Define an intermediate variable
- $ temp=0;
- //Number of outer loops
- for($i=0;$i //Two arrays adjacent to the right between the inner layers Compare the element values
- for($j=0;$j //When the value of the next array element is greater than the original value of the previous array
- if($arr [$j]>$arr[$j+1]){
- //Array element exchange
- $temp=$arr[$j];
- $arr[$j]=$arr[$j+1];
- $arr[$j+1]=$temp;
- }
- }
- //The number of times the outer array loop is ---count($arr)-1
- //The reason for -1 (array element value comparison It is two comparisons
- //For example, three array elements are compared twice
- echo "This is the result of the ".($i+1)."th comparison";
- echo "";
- print_r($arr);
- echo "";
- }
- echo "
";
- echo "";
- print_r($arr) ;
- echo "";
- ?>
Copy code
|