-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathburbuja.java
More file actions
42 lines (30 loc) · 1.23 KB
/
Copy pathburbuja.java
File metadata and controls
42 lines (30 loc) · 1.23 KB
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
35
36
37
38
39
40
41
42
import java.util.Scanner;
import javax.swing.JOptionPane;
public class burbuja{
public static void main(String[] args) { //main: atajo
Scanner entrada= new Scanner(System.in);
int arreglo[],n_elementos,aux;
n_elementos = Integer.parseInt(JOptionPane.showInputDialog("digite cantidad de elementos del arreglo"));
arreglo = new int[n_elementos];//le asignamos nros elementos al arreglo
for (int i = 0; i < n_elementos; i++) {
System.out.println((i+1)+" digite un numero");
arreglo[i]=entrada.nextInt();
}
//metodo burbuja
for (int i = 0; i < (n_elementos-1); i++) {
for (int j = 0; j < (n_elementos-1); j++) {
if (arreglo[j] > arreglo[j+1]) {
aux = arreglo[j];
arreglo[j] = arreglo[j+1];
arreglo[j+1]=aux;
}
}
}
//mostrando el arreglo de forma creciente
System.out.println("arreglo ordenado: ");
for (int i = 0; i < n_elementos; i++) {
System.out.println(arreglo[i]);
}
entrada.close();
}
}