Bubblesort
Login or Register to Bookmark this snippet
This is a simple bubblesort by Petter Nilsenthat takes 2 arrays as argument.The first one is the actual data used for sorting, the second is data that will "tag along" with the first array, for instance a descriptive text about the data in the first array.
<?php
function BubbleSort2($sort_array,$column = 0,$reverse) {
$lunghezza=
count($sort_array);
for ($i = 0; $i < $lunghezza ; $i++){
for ($j = $i + 1; $j < $lunghezza ; $j++){
if($reverse){
if ($sort_array[$i][$column] < $sort_array[$j][$column]){
$tmp = $sort_array[$i];
$sort_array[$i] = $sort_array[$j];
$sort_array[$j] = $tmp;
}
}else{
if ($sort_array[$i][$column] > $sort_array[$j][$column]){
$tmp = $sort_array[$i];
$sort_array[$i] = $sort_array[$j];
$sort_array[$j] = $tmp;
}
}
}
}
return $sort_array;
}
?>
Added by JC on 14th November, 2007
There are no comments about this snippet.
You must be registered and logged in to post a comment.
Login here to post a comment