[Java] what's wrong with my bubble sort algorithms?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
stonecoldgen
Messages
108
Reaction score
0
I have 2 possibilities, non of them worked:
PD: I am sure that all the methods inside this method(s) are correct and I am sure that the program's GUI is refreshing effectively.

Code:
 public void metodo1()
    {

    	for (int i=0; i<(perros.size()); i++){
    		for (int j=perros.size()-1; j>0; j--){
    			
    			
    			Perro perro2=(Perro)perros.get(j);
    			int pts2=perro2.darPuntosObediencia();
    			
    			Perro perro3=(Perro)perros.get(j-1);
    			int pts3=perro3.darPuntosObediencia();
    			
    			
    			if(pts2<pts3){
    				
    				perros.set((j-1), perro2);
    				perros.set(j, perro3);
    				
    			}
    			
    			
    		}
    	}
    }


Code:
    public void metodo1()
    {

             for (int j=0; j<(i-1); j++){ //empieza a la izquierda del arayList, ascendiendo hasta la posicion i-1

                
                Perro perroIzquierda=(Perro)perros.get(j); //lo mismo para el del indice j
                int ptsIzquierda=perroIzquierda.darPuntosObediencia();
                
                Perro perroMitad=(Perro)perros.get(j+1);
                int ptsMitad=perroMitad.darPuntosObediencia();
                
                
                if(ptsMitad<ptsIzquierda){ //si se da esta condicion:
                    
                perros.set ((j+1), perroIzquierda); //el perro de la izquierda sube una posicion
                
                
                }
                
            }
    }
 
Physics news on Phys.org


stonecoldgen said:
I have 2 possibilities, non of them worked:
PD: I am sure that all the methods inside this method(s) are correct and I am sure that the program's GUI is refreshing effectively.

Code:
 public void metodo1()
    {

    	for (int i=0; i<(perros.size()); i++){
    		for (int j=perros.size()-1; j>0; j--){
    			
    			
    			Perro perro2=(Perro)perros.get(j);
    			int pts2=perro2.darPuntosObediencia();
    			
    			Perro perro3=(Perro)perros.get(j-1);
    			int pts3=perro3.darPuntosObediencia();
    			
    			
    			if(pts2<pts3){
    				
    				perros.set((j-1), perro2);
    				perros.set(j, perro3);
    				
    			}
    			
    			
    		}
    	}
    }


Code:
    public void metodo1()
    {

             for (int j=0; j<(i-1); j++){ //empieza a la izquierda del arayList, ascendiendo hasta la posicion i-1

                
                Perro perroIzquierda=(Perro)perros.get(j); //lo mismo para el del indice j
                int ptsIzquierda=perroIzquierda.darPuntosObediencia();
                
                Perro perroMitad=(Perro)perros.get(j+1);
                int ptsMitad=perroMitad.darPuntosObediencia();
                
                
                if(ptsMitad<ptsIzquierda){ //si se da esta condicion:
                    
                perros.set ((j+1), perroIzquierda); //el perro de la izquierda sube una posicion
                
                
                }
                
            }
    }

I don't think you will get much help here. All of your variable names and comments are in Spanish, which makes your code more difficult to comprehend para esos que no intienden español.

What's more, both of your methods have the same name, metodo1. They should at least have different names. Also, why is the class named "Dog"? And a method named "giveObediencePoints"? What's up with that?

Good luck...
 


This looks like a homework problem: sort the dogs in an obedience competition by points scored using bubble sort. I guess darX() is a standard translation of getX().

I can't see anything wrong with the first method on visual inspection, and I don't have a compiler to check at the moment. I'll try to look this evening.

The second method never declares i. Presumably there is a class member of the same name if this is compiling. Also, the swap is incomplete.

If you are using a half-way modern Java, it's probably warning you about the declaration of the ArrayList perros. Try

ArrayList<Perro> perros;

This tells Java that the elements of perros are all instances of Perro, and you can lose all the casts on the get() calls.

Edit: Tried your first method at home and it works fine for me. Your second method is wrong for the reasons outlined above.
 
Last edited: