Last active
November 18, 2020 14:10
-
-
Save ultramarshall/81265ac8651acdf343e669020abf4831 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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