Skip to content

Instantly share code, notes, and snippets.

@fernandosavio
Last active August 29, 2015 13:56
Show Gist options
  • Save fernandosavio/9050284 to your computer and use it in GitHub Desktop.
Save fernandosavio/9050284 to your computer and use it in GitHub Desktop.
Bubble Sort implementado em PHP com flag se o array já estiver ordenado
<?php
/**
* Bubble Sort implementado em PHP com flag se o array já estiver ordenado
*/
function bubble_sort($arr) {
$flag = false;
while(!$flag) {
$flag = true;
for ($i=1; $i < count($arr); $i++) {
if($arr[$i-1] > $arr[$i]){
$flag = false;
$aux = $arr[$i];
$arr[$i] = $arr[$i-1];
$arr[$i-1] = $aux;
}
}
}
return $arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment