• Lenguaje

    Java

  • Descripción

    Pide al usuario 10 nombres, los guarda en un arreglo, ordena el arreglo en orden alfabético con el método de la burbuja y muestra el arreglo ordenado.

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
import java.util.Scanner;

public class Nombres {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int i, j;
        String nombre;
        String[] nombres = new String[10];
        for (i=0; i<nombres.length; i++)
        {
            System.out.print("Ingrese el nombre " + (i+1) + ": ");
            nombres[i] = in.nextLine();
        }
        for (i=1; i<nombres.length; i++)
            for (j=0; j<nombres.length-i; j++)
                if (nombres[j].compareTo(nombres[j+1]) > 0)
                {
                    nombre = nombres[j];
                    nombres[j] = nombres[j+1];
                    nombres[j+1] = nombre;
                }
        System.out.println("\nNombres ordenados:\n");
        for (i=0; i<nombres.length; i++)
            System.out.println("Nombre " + (i+1) + ": " + nombres[i]);
        System.out.println();
    }

}