Skip to content

Instantly share code, notes, and snippets.

@krypton
Created August 24, 2012 16:55
Show Gist options
  • Save krypton/3452863 to your computer and use it in GitHub Desktop.
Save krypton/3452863 to your computer and use it in GitHub Desktop.
Bubble sort in PHP
function bubbleSort ($items) {
$size = count($items);
for ($i=0; $i<$size; $i++) {
for ($j=0; $j<$size-1-$i; $j++) {
if ($items[$j+1] < $items[$j]) {
arraySwap($items, $j, $j+1);
}
}
}
return $items;
}
function arraySwap (&$arr, $index1, $index2) {
list($arr[$index1], $arr[$index2]) = array($arr[$index2], $arr[$index1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment