DEV Community

loading...
Cover image for Day 8: Bubble Sort

Day 8: Bubble Sort

mattryanmtl profile image Matt Ryan Updated on ・1 min read

Bubble Sort algorithm using php.

Alt Text

<?php
function toggle_sort(&$a) {
    $i = 0;
    $lastindex = count($a) - 1;

    while ($i < $lastindex) {
        if ($a[$i] <= $a[$i+1]) {
            $i++;
        } else {
            $tmp = $a[$i];
            $a[$i] = $a[$i+1];
            $a[$i+1] = $tmp;
            if ($i) { $i--; }
        }
    }
}

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

toggle_sort($values);

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

Discussion (0)

Forem Open with the Forem app