Home  >  Article  >  Backend Development  >  Basic explanation of php bubble sorting

Basic explanation of php bubble sorting

小云云
小云云Original
2018-03-20 10:01:481363browse

Bubble Sort (Bubble Sort) is a relatively simple sorting algorithm in the field of computer science. It repeatedly walks through the sequence to be sorted, comparing two elements at a time and swapping them if they are in the wrong order. The work of visiting the array is repeated until no more exchanges are needed, which means that the array has been sorted. This article mainly shares with you the basic explanation of PHP bubble sorting, I hope it can help you.

function order($arr){

    $count = count($arr);

    for($a=0;$a<$count-1;$a++){

        for($i=0;$i<$count-$a-1;$i++){

            if($arr[$i]<$arr[$i+1]){

                $temp = $arr[$i+1];

                $arr[$i+1] = $arr[$i];

                $arr[$i] = $temp;

            }

        }

    }

        return $arr;

}


Related recommendations:

Detailed explanation of bubble sorting in sort sorting in JS

Detailed explanation of bubble sorting in JavaScript

Simple understanding of PHP bubble sorting

The above is the detailed content of Basic explanation of php bubble sorting. For more information, please follow other related articles on the PHP Chinese website!

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