-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bubble.java
55 lines (48 loc) · 1.88 KB
/
Bubble.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
//===========//
//BUBBLE SORT//
//===========//
package Sorting;
import java.util.Random;
public class Bubble {
public static void main(String[] args) {
System.out.println("\n#=============#");
System.out.println("| BUBBLE SORT |");
System.out.println("#=============#\n");
// criacao do array
int[] array = new int[10];
// preenchimento do array com numeros aleatorios de 10 a 99
for (int i = 0; i < array.length; i++){
array[i] = (int) (new Random().nextInt(90) + 10);
}
// imprime o vetor desordenado
System.out.println("Vetor desordenado:");
System.out.println("*-------------------------------------------------*");
for (int i = 0; i < array.length; i++){
System.out.print("| " + array[i] + " ");
}
System.out.println("|");
System.out.println("*-------------------------------------------------*\n");
//=====BUBBLE SORT=====//
// controlador
for (int i = array.length; i > 1; i--){
// comparador
for (int j = 0; j < i-1; j++){
// se o elemento a direita for menor, realiza o swap
if (array[j] > array[j+1]){
int aux = array[j];
array[j] = array[j+1];
array[j+1] = aux;
}
}
}
//=====BUBBLE SORT=====//
// imprime o vetor ordenado
System.out.println("Vetor ordenado apos o sort:");
System.out.println("*-------------------------------------------------*");
for (int i = 0; i < array.length; i++){
System.out.print("| " + array[i] + " ");
}
System.out.println("|");
System.out.println("*-------------------------------------------------*\n");
}
}