Bubble Sort Algorithm Using PHP

Here’s a snippet in sorting using bubble sort algorithm in PHP

<?php
$num = array(10, 1, 3, 4, 9, 7, 2, 5, 8);

var_dump($num);

for($i=0;$i<count($num)-1;$i++) {
  for($j=$i+1;$j$num[$j]) {
      $temp = $num[$i];
      $num[$i] = $num[$j];
      $num[$j] = $temp;
    }
  }
}

var_dump($num);

$num is the array of integers to be sorted. When sorting done, $num will contain sorted integers in ascending order.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

CAPTCHA Image

*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>