JavaScript - Ordenar numeros de un input con borbuja

 
Vista:

Ordenar numeros de un input con borbuja

Publicado por Manuela (3 intervenciones) el 20/01/2020 00:57:13
Buenas noches, debo ordenar números que ingrese el usuario con algoritmos de ordenamiento, por ejemplo borbuja que es el mas facil, pero no logro capturar bien los numeros.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<h1>Obtener el valor de un input type=text </h1>
 
    <form id="form1">
        Numeros:<br><input type="text" name="numeros" id="numeros" class="formulario">
    </form>
 
    <input type="button" value="obtener" onclick="capturar()">
    <div id="resultado"></div>
 
       <script>
 
 
        function capturar()
        {
            var porId=document.getElementById("numeros").value;
            var arr = porId.split(" ").map(el => parseInt(el));
 
            console.log("uno"+arr);
 
            for(let i = arr.length; i > 0; i--){
                for(let j = 0; j < i-1; j++){
                    if(arr[j] > arr[j+1]){
                        var temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
                }
            }
            document.getElementById("resultado").innerHTML = arr;
        }
 
    </script>
 
</body>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de juan jose
Val: 42
Ha aumentado 1 puesto en JavaScript (en relación al último mes)
Gráfica de JavaScript

Ordenar numeros de un input con borbuja

Publicado por juan jose (18 intervenciones) el 20/01/2020 07:43:59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</html>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title> numeros </title>
 
    <script>
 
    var arr = [];
    function capturar(){
        var porId=document.getElementById("numeros").value.replace(/ /g, '');
        arr[arr.length]=porId;
        document.getElementById("resultado").innerHTML = arr.sort();
    }
 
    </script>
</head>
<body>
 
    <h1>Obtener el valor de un input type=text </h1>
 
    <form id="form1">
    Numeros:<br><input type="text" name="numeros" id="numeros" class="formulario">
    </form>
 
    <input type="button" value="obtener" onclick="capturar()">
    <div id="resultado"></div>
 
</body>
</html>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar