Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bubble.sort Ranking from Lowest to Highest() Java

Good evening guys!
I’m doing the course homework and the teacher gave us an exercise in which we need to sort the Ranking. He orders the first 5 positions, we are in this impasse almost a week 🙁

Below follows the code:

for (int i = 1; i < valores.size (); i++) {
for (int k = i + 1; k < valores.size ()  -  1; k++) {
    if ((valores.get ( i )).compareTo ( valores.get ( k ) ) > 0) {
        Double temp1 = valores.get ( i );
        valores.set ( i, valores.get ( k ) );
        valores.set ( k, temp1 );
        Ouvinte temp = listaSorteio.get ( i );
        listaSorteio.set ( i, listaSorteio.get ( k ) );
        listaSorteio.set ( k, temp );
    }
}
System.out.println ( (i) + " - Valores: " + valores.get ( i ) + "\t\tPessoa: " + listaSorteio.get ( i ));

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Why do you do

for (int i = 1; i < valores.size (); i++) 

? The error come from that

Always remember that in an array the indice begin by 0 and finish by the length of the array-1…

for (int i = 0; i < valores.size (); i++) {}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading