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

In summary, the conversation is about a problem with two methods, both named "metodo1", that are meant to sort a list of dogs by their obedience points. One of the methods appears to be functioning correctly, while the other has some issues with variable names and a missing declaration for a variable. The code also includes some comments in Spanish, which may make it difficult for non-Spanish speakers to understand.
  • #1
stonecoldgen
109
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
  • #2


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...
 
  • #3


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:

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

1. Why is my bubble sort algorithm not sorting the array correctly?

There could be several reasons for this. One possibility is that there is a bug in your code, such as a typo or logical error. Another possibility is that you are not properly implementing the bubble sort algorithm, either by not swapping elements correctly or not looping through the array enough times.

2. How can I optimize my bubble sort algorithm for better performance?

One way to optimize a bubble sort algorithm is to add a flag that checks if any swaps were made during a pass through the array. If no swaps were made, then the array is already sorted and the algorithm can stop. Another optimization is to reduce the number of comparisons by starting the inner loop at the last sorted element instead of the first.

3. Why does my bubble sort algorithm have a time complexity of O(n^2)?

A bubble sort algorithm has a time complexity of O(n^2) because it has two nested loops. The outer loop runs n times, where n is the size of the array, and the inner loop runs n-1 times for the first iteration, then n-2 times for the second iteration, and so on. This results in n*(n-1) comparisons, which simplifies to O(n^2).

4. Can I use a bubble sort algorithm for sorting large datasets?

Bubble sort is not the most efficient sorting algorithm and is not recommended for large datasets. Its time complexity of O(n^2) means that the number of comparisons grows exponentially with the size of the input. Other sorting algorithms such as merge sort or quicksort are better suited for sorting large datasets.

5. Are there any advantages to using a bubble sort algorithm?

One advantage of using a bubble sort algorithm is that it is simple and easy to implement, making it a good choice for small datasets or when simplicity is more important than efficiency. It also has a space complexity of O(1) since it sorts the array in-place, meaning it does not require any additional memory. However, for larger datasets, other sorting algorithms are typically a better choice.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top