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

Click For Summary
SUMMARY

The discussion centers on issues with two bubble sort implementations in Java for sorting a list of "Perro" objects based on obedience points. The first method, while visually correct, is overshadowed by the second method's failure due to an undeclared variable 'i' and incomplete swap logic. Additionally, the use of the same method name "metodo1" for both implementations creates confusion. The suggestion to declare the ArrayList as ArrayList<Perro> perros; is highlighted to eliminate unnecessary casting.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with bubble sort algorithm
  • Knowledge of ArrayList data structure in Java
  • Basic object-oriented programming concepts
NEXT STEPS
  • Review Java ArrayList documentation for type safety
  • Learn about method overloading and naming conventions in Java
  • Study bubble sort algorithm implementation and optimization techniques
  • Explore debugging techniques for Java code to identify undeclared variables
USEFUL FOR

Java developers, computer science students, and anyone interested in sorting algorithms and object-oriented programming practices.

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
                
                
                }
                
            }
    }
 
Technology 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:

Similar threads

  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K