In the previous article "PHP Bubble Sorting Algorithm (1)", we combined specific code examples to introduce the PHP bubble sorting algorithm to everyone. Everyone has some understanding of this.
Now we will introduce to you how to debug the implementation process of running the bubble sort algorithm more intuitively through Xdebug.
The code is as follows:
<?php function maopao($arr){ $len = count($arr); for($k=0;$k<=$len;$k++) { for($j=$len-1;$j>$k;$j--){ if($arr[$j]<$arr[$j-1]){ $temp = $arr[$j]; $arr[$j] = $arr[$j-1]; $arr[$j-1] = $temp; } } } return $arr; } $arr = [8,2,34,5]; var_dump(maopao($arr));
First we create a breakpoint at the following location and run this code in the foreground.
#Then click the arrow that runs downward.
#Every time you go down a step, the running results of the current code segment will be debugged, as follows.
Compare two adjacent elements, temporarily assign a value and put the larger value in later.
By looping like this, you can get the sorting result of the bubble algorithm.
is as follows:
array (size=4) 0 => int 2 1 => int 5 2 => int 8 3 => int 34
Related recommendations: "PHPStorm How to configure the xdebug tool and use it"
This article is an introduction to the PHP bubble sort algorithm That's it, it's simple and easy to understand, I hope it will be helpful to friends in need!
The above is the detailed content of PHP bubble sort algorithm (2). For more information, please follow other related articles on the PHP Chinese website!