CoderZone.org

Category: >> PHP Code >> Bubblesort routine Bookmark and Share

<< lastnext >>

Snippet Name: Bubblesort routine

Description: Reasonably quick bubblesort routine.

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: March 01st, 2009

<? 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);
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 9