PHP's simple bubble code sharing

  • 2020-05-19 04:25:24
  • OfStack

Very basic things, feel that the code is not concise enough, I hope the master guide to modify
 
<?php 
function BubbleSort($str){ 
for($i=0;$i<count($str);$i++){// Take it from the end of the array 1 A value;  
for ($k=count($str)-2;$k>=$i;$k--){// I'm going to bubble this value forward;  
if($str[$k+1]<$str[$k]){ // If I change the less than sign to the greater than sign, it's in descending order;  
$tmp=$str[$k+1]; 
$str[$k+1]=$str[$k]; 
$str[$k]=$tmp; 
} 
} 
} 
return $str; 
} 
// Here's 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 turn, putting the decimal in the front and the large number in the back. That is, first compare the first number and the second number, put the decimal before and the large number after. Then compare the second number and the third number, put the decimal before, put the large number after, and so on, until you compare the last two Numbers, put the decimal before, put the large number after. , repeat the above process is still starting from the logarithmic 1 more (because the number of exchange may be due to the number 2 and 3, no longer makes 1 number less than the number 2) and put before the decimal number after put 1 straight compared to the maximum number of consecutive Numbers 1, put before the decimal number after put on 2 end, 1 second from bottom in the number of new maximum number. And so on, until the sorting is finally done.
Since the decimals move forward and the large Numbers move back in the sorting process, which is equivalent to the bubble rising, it is called bubble sorting.
The outer loop variable is set to i, and the inner loop variable is set to j. Repeat the outer loop 9 times, repeat the inner loop 9, 8,... 1 times. The two elements that are compared each time are related to the inner loop j, which can be identified by a[j] and a[j+1] respectively, and i is 1,2,... For every 1 i, j
The values are 1,2,... 10 - i.
 
<?php 
function asc($a) 
{ 
for($i=0;$i<count($a)-1;$i++) 
{ 
for($j=0;$j<count($a)-1;$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)); 
?> 
<br /> 
<?php 
function desc($a) 
{ 
$c=array(); 
for($i=count($a)-1;$i>0;$i--) 
{ 
for($j=0;$j<count($a)-1;$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); 
?>  

Demonstration of PHP bubble sort
In the past, when the interviewer gave the written test, he thought that compared with XX, the program should be written on the computer, not on the pen.
PHP program file sort_bubble_up.php
 
<html> 
<head><title> Bubble sort demonstration </title><head> 
<link rel="stylesheet" href="sort.css"> 
<body> 
<h1> Bubble sort demonstration </h1> 
<table cellpadding="5" cellspacing="1" border="0" align="center"> 
<?php 
// Randomly generated array  
$arr=array(); 
echo '<tr><td colspan="10" class="title"> The initial value </td></tr>'; 
echo '<tr>'; 
for($i=0;$i<10;$i++){ 
$arr[$i]=rand(); 
echo "<td>\$arr[$i]={$arr[$i]}</td>"; 
} 
// Let's do bubble sort  
for($i=9;$i>0;$i--){ 
echo '<tr><td colspan="10" class="title"> The first '.(10-$i).' time </td></tr>'; 
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 '<tr>'; 
for($k=0;$k<10;$k++){ 
switch($k){ 
case $j : echo '<td class="base">'; break; 
case $j+1 : echo '<td class="light">'; break; 
default : echo '<td>'; 
} 
echo "\$arr[$k]={$arr[$k]}</td>"; 
} 
echo '</tr>'; 
} 
} 
// Display sort results  
echo '<tr><td colspan="10" class="title"> The results of </td></tr>'; 
echo '<tr>'; 
for($i=0;$i<10;$i++){ 
echo "<td>\$arr[$i]={$arr[$i]}</td>"; 
} 
echo '</tr>'; 
?> 
</table> 
</body></html> 

The style sheet file sort.css
 
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;} 

Related articles: