Created
January 12, 2016 19:36
-
-
Save fransafu/34f9a30b2043c7a908fb to your computer and use it in GitHub Desktop.
Bubble Sort in PHP
This file contains hidden or 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
function bubbleSort($array){ | |
$lenArray = count($array); | |
for($i = 1; $i <= $lenArray; $i++){ | |
for($j = 0; $j < $lenArray; $j++){ | |
if (($j + 1) < $lenArray){ | |
if ($array[$j] > $array[$j + 1]){ | |
$aux = $array[$j]; | |
$array[$j] = $array[$j + 1]; | |
$array[$j + 1] = $aux; | |
} | |
} | |
} | |
} | |
return $array; | |
} | |
function main(){ | |
$array = array(4,3,5,2,1); | |
mostrarArray($array); | |
$array = bubbleSort($array); | |
mostrarArray($array); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment