programming-examples/php/Basics/PHP Program for Bubble sorting in PHP.php

24 lines
549 B
PHP
Raw Permalink Normal View History

2019-11-18 14:44:36 +01:00
// Write a Program for Bubble sorting in PHP
<?php
$numbers=array(12,23,45,20,5,6,34,17,9,56);
$n=count($numbers);
for ($c = 0 ; $c < ( $n - 1 ); $c++)
{
for ($d = 0 ; $d < ($n - $c - 1); $d++)
{
if ($numbers[$d] > $numbers[$d+1]) /* For decreasing order use < */
{
$swap = $numbers[$d];
$numbers[$d] = $numbers[$d+1];
$numbers[$d+1] = $swap;
}
}
}
echo "Sorted list in ascending order <br />";
for ( $c = 0 ; $c < $n ; $c++ )
echo $numbers[$c]." ";
?>