Home  >  Article  >  Backend Development  >  Bubble sorting method in php

Bubble sorting method in php

WBOY
WBOYOriginal
2016-08-08 09:21:291286browse
$arr=array(23,5,26,4,9,85,10,2,55,44,21,39,11,16,55,88,421,226,588);

$n = count($arr);/*count() function counts the number of cells in an array or the number of attributes in an object. For an array, return the number of its elements. For other values, return 1. If the argument is a variable and the variable is not defined, 0 is returned. If mode is set to COUNT_RECURSIVE (or 1), the number of elements in the array in the multidimensional array will be calculated recursively*/

//echo $n;

for($h=0;$h<$n -1;$h++){//Outer loop n-1

for($i=0;$i<$n-$h-1;$i++){

if($arr[$i]> ;$arr[$i+1]){//Judge the size of the array and reverse the position

$k/p> $arr[$i+1]=$arr[$i];

$arr[$i ]=$kong;

}

}

}

var_dump($arr);

?>Result: array(19) { [0]=> int(2) [ 1]=> int(4) [2]=> int(5) [3]=> int(9) [4]=> int(10) [5]=> int(11) [ 6]=> int(16) [7]=> int(21) [8]=> int(23) [9]=> int(26) [10]=> int(39) [ 11]=> int(44) [12]=> int(55) [13]=> int(55) [14]=> int(85) [15]=> int(88) [16]=> int(226) [17]=> int(421) [18]=> int(588) }

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the bubble sorting method of PHP, including the relevant content. 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
Previous article:unicode to ChineseNext article:unicode to Chinese