Home  >  Article  >  Backend Development  >  PHP simple bubble method code sharing_PHP tutorial

PHP simple bubble method code sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:16:36957browse

It’s very basic stuff. I feel that the code is not concise enough. I hope experts can guide me to modify it.

Copy the code The code is as follows:

function BubbleSort($str){
for($i=0;$ifor ($k =count($str)-2;$k>=$i;$k--){//Bubble this value forward;
if($str[$k+1]<$str[ $k]){ //Change the less than sign to the greater than sign, which is to sort in descending order;
$tmp=$str[$k+1];
$str[$k+1]=$str[$ k];
$str[$k]=$tmp;
}
}
}
return $str;
}
//The following is the test
$str=array(5,8,2,6,10,0,3,12,11);
print_r(BubbleSort($str));
?>

php bubble sort 2
The basic concept is: compare two adjacent numbers in sequence, put the decimal in the front and the large number in the back. That is, first compare the first and second numbers, put the decimal first and the large number last. Then compare the second number and the third number, put the decimal in front and the large number in the back, and continue like this until comparing the last two numbers, put the decimal in front and the large number in the back. Repeat the above process, still starting from the first pair of numbers (because it may be due to the exchange of the second number and the third number that the first number is no longer smaller than the second number), put the decimal first and the large number first. Finally, compare until a pair of adjacent numbers before the maximum number, put the decimal in front and the large number in the back. The second pass ends and a new maximum number is obtained in the penultimate number. Continue like this until the sorting is finally completed.
Because in the sorting process, decimals are always placed forward and large numbers are placed backward, which is equivalent to bubbles rising, so it is called bubble sorting.
Implemented using a double loop, with the outer loop variable set to i and the inner loop variable set to j. The outer loop is repeated 9 times, and the inner loop is repeated 9, 8,..., 1 time. The two elements compared each time are related to the inner loop j. They can be identified by a[j] and a[j+1] respectively. The values ​​​​of i are 1, 2,...,9. For The
value of each i, j is 1, 2,...10-i.
Copy code The code is as follows:

function asc($a)
{
for($i=0;$i{
for($j=0;$j{
if($a[$j]>$a[$j+1])
{
$tmp=$a[$j+1];
$ a[$j+1]=$a[$j];
$a[$j]=$tmp;
}
}
}
print_r($a);
}
$a = array(9,8,17,6,26,4,33,2,1);
print_r(asc($a));
?>


function desc($a)
{
$c=array();
for($i=count($a) -1;$i>0;$i--)
{
for($j=0;$j{
if( $a[$j]<$a[$j+1])
{
$tmp=$a[$j+1];
$a[$j+1]=$a [$j];
$a[$j]=$tmp;
}
}
}
print_r($a);
}
$arr=array (33,24,56,55,59);
desc($arr);
?>

PHP bubble sort method demonstration
In previous interviews, I felt that the written test questions asked by the examiner were quite XX, and the program should be written on the computer, not on the pen.
PHP program file sort_bubble_up.php
Copy code The code is as follows:


Bubble sort demonstration


Bubble sorting demonstration



//Randomly generate array
$arr=array();
echo '';
echo '';
for($i=0;$i<10;$i++){
$arr[$i]=rand();
echo "";
}
//Perform bubble sorting
for($i=9;$i>0;$i--){
echo '';
for($j=0;$j<$i;$j++){
if($arr[$j]<$arr[$j+1]){
$tmp=$arr[$j];
$arr[$j]=$arr[$j+ 1];
$arr[$j+1]=$tmp;
}
echo '';
for($k=0;$k<10;$k++ ){
switch($k){
case $j : echo '';
}
}
//Display sorting results
echo '';
echo '';
for($i=0;$i<10;$i++ ){
echo "";
}
echo '';
?>
Initial value
$arr[$i]={$arr[$i]}
th'.(10-$i).'time
'; break;
case $j+1 : echo '
'; break;
default : echo '
';
}
echo "$arr[$k]={$arr[$k]}}
echo '
result
$arr[$i]={$arr[$i]}



Style sheet file sort.css
Copy code The code is as follows:

h1{text-align: center; color: blue;}
table{font-size: 12px; font-family : arial; background-color: black; text-align: center;}
td{background-color: white;}
.base{background-color: #0FF;}
.light{background- color: #0DD;}
.title{background-color: #3FF; text-align: center;}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325904.htmlTechArticleIt’s very basic. I feel that the code is not concise enough. I hope an expert can guide me to modify and copy the code. The code is as follows: ?php function BubbleSort ($str){ for($i=0;$icount($str);$i++){//Get from the end of the array...
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