PHP bubble sort algorithm (2)

藏色散人
Release: 2019-02-28 09:09:27
Original
9941 people have browsed it

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.

PHP bubble sort algorithm (2)

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));
Copy after login

First we create a breakpoint at the following location and run this code in the foreground.

PHP bubble sort algorithm (2)

#Then click the arrow that runs downward.

PHP bubble sort algorithm (2)

#Every time you go down a step, the running results of the current code segment will be debugged, as follows.

PHP bubble sort algorithm (2)


PHP bubble sort algorithm (2)

Compare two adjacent elements, temporarily assign a value and put the larger value in later.

PHP bubble sort algorithm (2)

PHP bubble sort algorithm (2)

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
Copy after login

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!

Related labels:
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