Bubblesort Routine

Description: Reasonably quick bubblesort routine.

<? php
 
FUNCTION BubbleSort($sort_array,$reverse)
{
FOR ($i = 0; $i < SIZEOF($sort_array); $i++){
FOR ($j = $i + 1; $j < SIZEOF($sort_array); $j++){
IF($reverse){
IF ($sort_array[$i] < $sort_array[$j]){
$tmp = $sort_array[$i];
$sort_array[$i] = $sort_array[$j];
$sort_array[$j] = $tmp;
}
}ELSE{
IF ($sort_array[$i] > $sort_array[$j]){
$tmp = $sort_array[$i];
$sort_array[$i] = $sort_array[$j];
$sort_array[$j] = $tmp;
}
}
}
}
RETURN $sort_array;
}
?>
 
//Use like this :
//$array = array{10,65,32,41,1,99};
//$sorted = BubbleSort($array,0);

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.