Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I sort an array of chunks by doing this:

    for (int i =0; i<this.getQueue().size();i++) {
        for (int j =0; j<this.getQueue().size()-i-1;j++) {
            Chunk temp1 = this.getQueue().get(i);
            Chunk temp2 = this.getQueue().get(i+1);
                if (temp1 != null &&temp2 != null && temp2.getLocation().getY() < temp1.getLocation().getY()) {
                    this.getQueue().set(i, temp2);
                    this.getQueue().set(i+1, temp1);
                }
            }
        }

What I want is the the chunks with the lowest Y coordinate will be at the start of the array and the ones with the bigger Y coordinate will be at the end of the array. And this is my result:

1024.0
944.0
1104.0
944.0
1104.0
----BEFORE-----
944.0
1024.0
944.0
1104.0
1104.0
---AFTER---

Why is this not working It seams fine. I dont want to use a comparator so dont suggest it.

More info, the Y cords are floats. I got the result by for each looping on this queue and printed the Y locations.

share|improve this question

closed as too localized by Byte56, Jonathan Hobbs, Patrick Hughes, Laurent Couvidou, Nicol Bolas Sep 3 '12 at 14:39

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Thanks for the downvotes! –  Trixmix Aug 31 '12 at 23:52
1  
The reason you're getting downvotes is probably because there's nothing game-specific about your question. I've flagged it for migration to Stack Overflow, where it should be on topic. –  Ilmari Karonen Sep 2 '12 at 17:14
add comment

2 Answers 2

up vote 5 down vote accepted

Your inner loop iterates j times correctly, but the indexes you compare do not change within that loop. Therefore it's impossible for any value to bubble more than 1 position along. Instead of .get(i) and .get(i+1), you probably need .get(j) and .get(j+1).

You can usually debug problems like this with a debugger, or with System.out.println statements to show the values of i at each step.

share|improve this answer
    
Thanks sorry I was stupid I didn't notice that lol XD –  Trixmix Aug 31 '12 at 23:48
    
And its not i+j its just j. i+j doesn't make sense. –  Trixmix Aug 31 '12 at 23:49
    
In this case you're right, but i+j would make sense if you were bubbling values in the other direction. I've edited the answer. –  Kylotan Sep 3 '12 at 13:40
add comment

Probably more efficient to swap once per loop by first making a record of best index to swap. Also start the j loop with i + 1 instead of 0. This makes it easier to understand you are swapping starting 1 above the current index. Untested but should give you an idea of how this could be approached.

int qSize = this.getQueue().size();
for (int i = 0; i < qSize; i++) {
        Chunk temp1 = this.getQueue().get(i);
        if (temp1 != null) {
            int bestY = temp1.getLocation().getY();
            int swapWithIndex = -1; 
            for (int j =i + 1; j < qSize; j++) {
                  Chunk temp2 = this.getQueue().get(j);
                  if (temp2 != null && temp2.getLocation().getY() < bestY) {
                      bestY = temp2.getLocation().getY();
                      swapWithIndex = j;
                  }
            }

            if (swapWithIndex != -1) {
                 this.getQueue().set(i, this.getQueue().get(swapWithIndex));
                 this.getQueue().set(swapWithIndex, temp1);
            }
        }
    }
}
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.