Ver Mensaje Individual
  #5 (permalink)  
Antiguo 07/10/2008, 16:27
jamer007
 
Fecha de Ingreso: septiembre-2008
Mensajes: 32
Antigüedad: 15 años, 7 meses
Puntos: 0
Respuesta: problema en burbuja

Encontré este ejemplo tal vez te sirva para entender el metodo de ordenamiento Burbuja.

Código:
<?php
function form(){
    echo "<form action=\"".$_SERVER["PHP_SELF"]." \"name=\"form\" method=\"post\">";
    echo "<p>Por favor indica el número de digitos que ingresarás</p>";
    echo "<input type=\"text\" size=\"2\" maxlenght=\"2\" name=\"digitos\" />";
    echo "<input type=\"submit\" name=\"submit\" value=\"Aceptar\" /></form>";    
}

if(isset($_POST["enviar"])){
for($i=0;$i<$_POST["digitos"];$i++){
    for($a=0;$a<$_POST["digitos"];$a++){
        if($valores[$a]>$valores[$a+1]){
            $tmp=$valores[$a+1];
            $valores[$a+1]=$valores[$a];
            $valores[$a]=$tmp;
        }
    }
}
for($i=0;$i<=$_POST["digitos"];$i++){
    echo $valores[$i]."<br />";
}

}
if(isset($_POST["submit"])){
    echo "<form action=\"".$_SERVER["PHP_SELF"]." \"name=\"form\" method=\"post\">";
    echo "<p>Por favor llena el arreglo con valores numéricos</p>\n";
    echo "<table>\n<tr><td>\n";
    for($i=1;$i<=$digitos;$i++){
        echo "<input type=\"text\" size=\"2\" maxlenght=\"2\" name=\"valores[]\" />\n";    
        if(($i%10)==0){
            echo "</td></tr>\n";
            echo "<tr><td>";
        }
    }
    echo "<input type=\"submit\" name=\"enviar\" value=\"Enviar\" />\n";
    echo "<input type=\"hidden\" name=\"digitos\" value=\"".$_POST["digitos"]."\" />";
    echo "</td></tr></table>\n";
    echo "</form>";        
}
else{
    form();
}
?>