Advertisement
Guest User

Orden. Burbuja

a guest
Aug 23rd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1.  
  2. package arreglo;
  3.  
  4. import java.util.Scanner;
  5. import javax.swing.JOptionPane;
  6.  
  7. public class arreglo {
  8. public static void main(String[] args) {
  9.  
  10. //Entrada de datos
  11. Scanner entrada = new Scanner (System.in);
  12.  
  13. //Definicion de variables
  14. int arreglo[], nElementos, auxiliar;
  15.  
  16. //Usuario ingresa datos
  17. nElementos = Integer.parseInt(JOptionPane.showInputDialog("Por favor digite la cantidad de elementos"));
  18. arreglo = new int [nElementos];
  19.  
  20. for(int i=0; i < nElementos; i++ ){
  21. System.out.println((i+1)+". Ingrese el elemento:");
  22. arreglo[i] = entrada.nextInt();
  23.  
  24. }
  25. //Muestra del arreglo
  26. for (int i=0; i < nElementos; i++){
  27. System.out.print(arreglo[i]+" - ");
  28. }
  29.  
  30. //Método Burbuja
  31. for(int i = 0; i < nElementos; i++){
  32. for(int j = 0; j < nElementos; j++){
  33. if (arreglo[j] > arreglo[j+1]){
  34. auxiliar = arreglo[j];
  35. arreglo[j] = arreglo[j+1];
  36. arreglo[j+1] = auxiliar;
  37.  
  38. }
  39. }
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement