Skip to content

Instantly share code, notes, and snippets.

@ultramarshall
Last active November 18, 2020 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ultramarshall/81265ac8651acdf343e669020abf4831 to your computer and use it in GitHub Desktop.
Save ultramarshall/81265ac8651acdf343e669020abf4831 to your computer and use it in GitHub Desktop.
<?php
function bubble_sort($arr) {
$size = count($arr)-1;
for ($i=0; $i<$size; $i++) {
for ($j=0; $j<$size-$i; $j++) {
$k = $j+1;
if ($arr[$k] < $arr[$j]) {
list($arr[$j], $arr[$k]) = array($arr[$k], $arr[$j]);
}
}
}
return $arr;
}
function printArray($arr) {
for ($i = 0; $i < count($arr); $i++) {
echo $arr[$i] . " ";
}
}
$arr = array(99, 2, 64, 8, 111, 33, 65, 11, 102, 50);
echo "sebelum sorting " . '<br>';
printArray($arr);
echo '<br>';
echo '<br>';
echo "sesudah sorting " . '<br>';
$arr = bubble_sort($arr);
printArray($arr);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment