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

AI Thread Summary
The discussion centers around two Java methods intended to sort a list of "Perro" (dog) objects based on obedience points. The first method uses a bubble sort approach, iterating through the list and swapping elements when necessary. The second method also aims to sort but has issues, including an undeclared variable 'i' and an incomplete swap logic. Participants note that both methods share the same name, which is problematic, and suggest renaming them for clarity. There are also comments on the use of Spanish variable names and class naming conventions, which may hinder understanding for non-Spanish speakers. One user successfully tested the first method, confirming its functionality, while identifying flaws in the second method. Additionally, a suggestion is made to specify the type of the ArrayList to eliminate unnecessary casting.
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
13
Views
4K
Replies
3
Views
2K
Back
Top