Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jan 30, 2013 #1
    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 (Text):
     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();
                        perros.set((j-1), perro2);
                        perros.set(j, perro3);

    Code (Text):
        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
  2. jcsd
  3. Jan 31, 2013 #2


    Staff: Mentor

    Re: what's wrong with my bubble sort algorithms?

    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...
  4. Jan 31, 2013 #3


    User Avatar
    Science Advisor

    Re: what's wrong with my bubble sort algorithms?

    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: Jan 31, 2013
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: [Java] what's wrong with my bubble sort algorithms?
  1. Sorting algorithm? (Replies: 2)

  2. [Java] Selection Sort (Replies: 4)