DEV Community

loading...
Cover image for Day 24: Shell Sort

Day 24: Shell Sort

mattryanmtl profile image Matt Ryan ・1 min read
<?php
function shell_sort(&$a) {
    $count = count($a);

    $columns = 1;
    while ($columns < $count) {
        $columns = $columns * 2 + 1;
    }

    $columns = ($columns - 1) / 2;
    while ($columns > 0) {
        for ($c = 0; $c < $columns; $c++) {
            for ($i = $columns; $i < $count; $i += $columns) {
                $value = $a[$i];
                for ($x = $i - $columns;( ($x >= 0) && ($a[$x] > $value) );
                        $x -= $columns) {
                    $a[$x + $columns] = $a[$x];
                }
                $a[$x + $columns] = $value;
            }
        }
        $columns = ($columns - 1) / 2;
    }
}

$values = array(8, 2, 5, 9, 1);

shell_sort($values);

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

Discussion (0)

Forem Open with the Forem app