DEV Community

loading...
Cover image for Day 21: Insertion Sort

Day 21: Insertion Sort

mattryanmtl profile image Matt Ryan ・1 min read

Insertion Sort algorithm using php.

Alt Text

<?php
function insertion_sort(&$a) {
    $count = count($a);
    for ($i = 0; $i < $count; $i++) {
        $value = $a[$i];

        for ($x = $i - 1; ( ($x >= 0) && ($a[$x] > $value) ); $x--) {
            $a[$x + 1] = $a[$x];
        }
        $a[$x + 1] = $value;
    }
}

$values = array(2, 8, 4, 7, 6);

insertion_sort($values);

foreach ($values as $v) { echo "{$v} "; }
?>
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app