Skip to content

Commit 13dc337

Browse files
authored
Merge pull request kelvins#193 from AmanRawat33/patch-2
Update BubbleSort.java
2 parents 9bdfb92 + 0c30c39 commit 13dc337

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

src/java/BubbleSort.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,49 @@ public class BubbleSort {
22

33
public static void main(String[] args) {
44

5-
int vetor[] = {9, 0, 4, 2, 3, 8, 7, 1, 6, 5};
5+
int vector[] = {9, 0, 4, 2, 3, 8, 7, 1, 6, 5};
66

77
System.out.println("Bubble Sort:");
8-
System.out.println("Vetor não ordenado:");
9-
mostraVetor(vetor);
8+
System.out.println("unordered vector:");
9+
showvector(vector);
1010

11-
vetor = bubbleSort(vetor);
11+
vector = bubbleSort(vector);
1212

13-
System.out.println("Vetor ordenado:");
14-
mostraVetor(vetor);
13+
System.out.println("ordered vector:");
14+
showvector(vector);
1515
}
1616

17-
public static int[] bubbleSort(int vetor[]) {
18-
int trocas = 0;
19-
for (int i = 0; i < vetor.length - 1; i++) {
20-
if (vetor[i] > vetor[i + 1]) {
21-
int temp = vetor[i];
22-
vetor[i] = vetor[i + 1];
23-
vetor[i + 1] = temp;
24-
trocas++;
17+
public static int[] bubbleSort(int vector[]) {
18+
int exchange = 0;
19+
for (int i = 0; i < vector.length - 1; i++) {
20+
if (vector[i] > vector[i + 1]) {
21+
int temp = vector[i];
22+
vector[i] = vector[i + 1];
23+
vector[i + 1] = temp;
24+
exchange++;
2525
}
2626
}
27-
if (trocas != 0) bubbleSort(vetor);
28-
return vetor;
27+
if (exchange != 0) bubbleSort(vector);
28+
return vector;
2929
}
3030

31-
public static int[] bubbleSort2(int vetor[]) {
32-
for (int i = 1; i < vetor.length; i++) {
33-
int tam = vetor.length;
31+
public static int[] bubbleSort2(int vector[]) {
32+
for (int i = 1; i < vector.length; i++) {
33+
int tam = vector.length;
3434
for (int j = 0; j < tam - i; j++) {
35-
if (vetor[j] > vetor[j + 1]) {
36-
int temp = vetor[j];
37-
vetor[j] = vetor[j + 1];
38-
vetor[j + 1] = temp;
35+
if (vector[j] > vector[j + 1]) {
36+
int temp = vector[j];
37+
vector[j] = vector[j + 1];
38+
vector[j + 1] = temp;
3939
}
4040
}
4141
}
42-
return vetor;
42+
return vector;
4343
}
4444

45-
public static void mostraVetor(int vetor[]) {
46-
for (int i = 0; i < vetor.length; i++) {
47-
System.out.print(vetor[i] + ", ");
45+
public static void showvector(int vector[]) {
46+
for (int i = 0; i < vector.length; i++) {
47+
System.out.print(vector[i] + ", ");
4848
}
4949
System.out.println("");
5050
}

0 commit comments

Comments
 (0)