Insertion Sort Algorithm in 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($j=1;$j<count($num);$j++)
{
     $key=$num[$j];
     $i=$j-1;
     while($num[$i]=0)
     {
	    $num[$i+1]=$num[$i];
	 	$i--;
     }
     $num[$i+1]=$key;
}

var_dump($num);

$num is the array of integers to be sorted. When sorting done, $num will contain sorted integers in descending 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>