Bubble sorting method in php
Release: 2016-08-08 09:21:29
Original
1247 people have browsed it
$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 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
Latest Articles by Author
-
2024-08-16 22:38:11
-
2024-08-16 22:38:03
-
2024-08-16 22:35:17
-
2024-08-16 22:07:03
-
2024-08-16 22:04:32
-
2024-08-16 22:04:06
-
2024-08-16 22:04:03
-
2024-08-16 22:03:33
-
2024-08-16 21:45:12
-
2024-08-16 21:40:44