1
Write a program that loads a vector with 10 integers and sort it in ascending order using the Bubble Sort method.
The program must generate a second vector without repeated numbers.
package com.bubble.sort;
import java.util.Scanner;
public class bubble_sort {
private static Scanner teclado;
public static void main(String args []) {
int vet1[] = new int [10];
int vet2[] = new int [10];
int n, i, aux,troca;
teclado = new Scanner(System.in);
//Carregando os números do vetor:
for(i=0;i<=9;i++) {
System.out.print("Digite "+(i+1)+"º número do vetor:");
vet1[i]= teclado.nextInt();
vet2[i] = vet1[i];
}
n = 1;
troca = 1;
while (n <= 10 && troca == 1) {
troca = 0;
for(i=0;i<=8;i++) {
if(vet1[i] > vet1[i+1]) {
troca = 1;
aux = vet1[i];
vet1[i] = vet1[i+1];
vet1[i+1] = aux;
}
}
n = n + 1;
}
for(i=0;i<=9;i++) {
System.out.print("["+vet1[i]+"]");
}
}
}
It’s getting the same vector.
– Washington Klébio
The second verse?
– Wictor Chaves
Could make the full code?
– Washington Klébio
Complete code posted.
– Wictor Chaves
There is no way to take the 0 in the repeated position not right?
– Washington Klébio
made the change, now will not show the values 0
– Wictor Chaves
And you’re losing the first value every time!
– Washington Klébio
Dude, I made a correction, now you’re getting the first number. @Kuriôzu
– Wictor Chaves
In mine it’s the same.
– Washington Klébio
The modification was very small, you took the new code?
– Wictor Chaves
yes, but still not showing the 1st value of the 2 generated vector.
– Washington Klébio
Try it now, please.
– Wictor Chaves
Now the first one appears, but a number appears after the numbers of the vector: EX.: [1],[2],[1]
– Washington Klébio
Okay buddy, sorry about this much change is because I’m in the service and I don’t have an ide here, but I believe it will now work 100%. @Kuriôzu
– Wictor Chaves
100%, dude helped a lot.
– Washington Klébio
Mark the answer as answered, please.
– Wictor Chaves