$size = sizeof($arr)-1; for ($i = $size; $i>=0; $i--) { for ($j = 0; $j<=($i-1); $j++) if ($arr[$j]>$arr[$j+1]) { $k = $arr[$j]; $arr[$j] = $arr[$j+1]; $arr[$j+1] = $k; } }
$SIZE = SIZEof($A_R__R)-1; for ($i = $SIZE; $i>=0; $i--) { for ($j = 0; $j<=($i-1); $j++) if ($A_R__R[$j]>$A_R__R[$j+1]) { // $A_R__R[$j+1] = $A_R__R[$j]; + $A_R__R[$j+1]; $A_R__R[$j] = $A_R__R[$j+1] - $A_R__R[$j]; $A_R__R[$j+1] = $A_R__R[$j+1] - $A_R__R[$j]; } }
$size = sizeof($arr)- 1 ; for ($INDEX_I = $size; $INDEX_I>=0 ; $INDEX_I--){ for ($INDEX_J = 0; $INDEX_J<=($INDEX_I-1) ; $INDEX_J++) if( $arr[$INDEX_J] > $arr[$INDEX_J+1]){ $k = $arr[$INDEX_J] ; $arr[$INDEX_J] = $arr[$INDEX_J+1] ; $arr[$INDEX_J+1] = $k ; } }
for($i=sizeof($M)-1;$i>=0;$i--){ for($j=0;$j<=($i-1);$j++) if($M[$j]>$M[$j+1]) { list($M[$j],$M[$j+1])=[$M[$j+1],$M[$j]]; } }
<?php $error = null; $query = 'SELECT * FROM prices'; $result = mysql_query($query) or $error = ' : ' . mysql_error(); $arr = []; while ($line = mysql_fetch_row($result)) { $arr[] = $line['price']; } $size = sizeof($arr)-1; for ($i = $size; $i>=0; $i--) { for ($j = 0; $j<=($i-1); $j++) if ($arr[$j]>$arr[$j+1]) { $k = $arr[$j]; $arr[$j] = $arr[$j+1]; $arr[$j+1] = $k; } } echo "<ul>"; foreach ($arr => $v) echo "<li>".$v."</li>"; echo "</ul>"; ?> <script> var error = "<?php echo $error?>"; if (error) alert("ERROR: " + error); </script>
<?php require_once 'Sorter/Bubble/NumericArray.php'; require_once 'Storage/Array.php'; $bubbleSorter = new Sorter_Bubble_NumericArray(); $storage = new Storage_Array; $storage->setElements(array(15, 204, 1, 4, 2, 6, 8, 3, 150)); $bubbleSorter->sort($storage); var_dump($storage->getElements());
<?php require_once 'Sorter/Interface.php'; abstract class Sorter_Bubble_Abstract implements Sorter_Interface { /** * * @var Comparator_Interface */ protected $_comparator; /** * * @var Swapper_Interface */ protected $_swapper; /** * * @var Storage_Interface */ protected $_storage; /** * */ public function _bubble() { for ($i = $this->_storage->getSize() - 1; $i >= 0; $i--) { for ($j = 0; $j <= ($i - 1); $j++) { if ($this->_comparator->isBigger($this->_storage->getElementByIndex($j + 1), $this->_storage->getElementByIndex($j))) { $this->_swapper->swap($this->_storage, $j + 1, $j); } } } } }
<?php require_once 'Sorter/Bubble/Abstract.php'; require_once 'Storage/Interface.php'; require_once 'Comparator/Numeric.php'; require_once 'Swapper.php'; class Sorter_Bubble_NumericArray extends Sorter_Bubble_Abstract { public function sort(\Storage_Interface &$storage) { $this->_storage = $storage; $this->_comparator = new Comparator_Numeric(); $this->_swapper = new Swapper(); $this->_bubble(); } }
$size= sizeof($arr)- 1; for($i =$size;$i>=0; $i--) { for($j =0;$j<=($i-1); $j++) if($arr[$j] > $arr[$j+1]) { $k= $arr[$j]; $arr[$j]= $arr[$j+1]; $arr[$j+1]= $k; } }
$CocaCola = sizeof($Elvis)-1; // <<---- for ($i = $CocaCola; $i>=0; $i--) { for ($j = 0; $j<=($i-1); $j++) if ($Elvis[$j]>$Elvis[$j+1]) { // , $Presley = $Elvis[$j]; $Elvis[$j] = $Elvis[$j+1]; $Elvis[$j+1] = $Presley; } }
Source: https://habr.com/ru/post/225827/
All Articles