Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Last active December 12, 2015 12:09
Show Gist options
  • Select an option

  • Save ackintosh/4770442 to your computer and use it in GitHub Desktop.

Select an option

Save ackintosh/4770442 to your computer and use it in GitHub Desktop.
Bubble sort in PHP
<?php
$array = range(1, 20);
shuffle($array);
var_dump($array);
var_dump(bubblesort($array));
function bubblesort($array)
{
$size = count($array);
for ($i = $size; $i > 0; $i--) {
for ($j = 1; $j < $i; $j++) {
if ($array[$j - 1] > $array[$j]) {
list($array[$j - 1], $array[$j]) = array($array[$j], $array[$j - 1]);
}
}
}
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment