Skip to content

Instantly share code, notes, and snippets.

@fransafu
Created January 12, 2016 19:36
Show Gist options
  • Save fransafu/34f9a30b2043c7a908fb to your computer and use it in GitHub Desktop.
Save fransafu/34f9a30b2043c7a908fb to your computer and use it in GitHub Desktop.
Bubble Sort in PHP
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