codice:
//--------------------------------------------
function incision_sort($arr, $col){
	//artox at online dot no
       for($k = 0; $k < sizeof($arr)-1; $k++){
           // $arr[$k+1] is possibly in the wrong place. Take it out.
           $t = $arr[$k+1];
           $i = $k;   
          
           // Push $arr[i] to the right until we find the right place for $t.
           while($i >= 0 && $arr[$i][$col] > $t[$col]){
               $arr[$i+1] = $arr[$i];
               $i--;
           }
          
           // Insert $t into the right place.
           $arr[$i+1] = $t;                           
       }// End sort
       return $arr;       
   }
//--------------------------------------------
function selecta($temp,$tabella){
	$sql="SELECT * FROM $tabella";
	$st=mysql_query($sql) or die(mysql_error());
	while ($row=mysql_fetch_assoc($st)){
		array_unshift($row,$tabella);
		$temp[]=$row;
	}
	return $temp;
} 
//--------------------------------------------
$temp=array();
$temp=selecta($temp,"tabella1");
$temp=selecta($temp,"tabella2");
$temp=incision_sort($temp,"campo3");
$temp=array_slice($temp,-3,3);
$temp=array_reverse($temp);
foreach($temp as $valori){
	foreach($valori as $valore)
		echo $valore." ";
	echo "
\n";
}