Php program for insertion sort, Easy implementation

insertion-sort

Php program for insertion sort. Easy implementation.

here is the link just try once https://github.com/vishvendrasingh/insertion_sort

<?php
/**Function for sorting an array with insertion sort algorithm.
*
* @param array $array
* @return array
*/
function insertionSort(array $array) {
$length=count($array);
for ($i=1;$i<$length;$i++) {
$element=$array[$i];
$j=$i;
while($j>0 && $array[$j-1]>$element) {
//move value to right and key to previous smaller index
$array[$j]=$array[$j-1];
$j=$j-1;
}
//put the element at index $j
$array[$j]=$element;
}
return $array;
}
$array = range(0, 19);
shuffle($array);
var_dump($array);
$arr = insertionSort($array);
var_dump($arr);
?>

here is the link just try once https://github.com/vishvendrasingh/insertion_sort

0

Leave a Reply

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

*