0

I am trying to use the summer to practice more Java to get better by learning how to code algorithms. I have this problem where I add elements to my ArrayList but somehow the first number I add also sets the number of positions in my list which I want to avoid. I only want the 0th index to contain the number 5. I seem to not catch a clue on how to solve this.

public class Algorithms {

private ArrayList<Integer> numbers;

public Algorithms() {

    numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(4);
    bubblesort();
}

public static void main(String args[]) {
    new Algorithms();
}

public void bubblesort() {

    System.out.println(numbers);
    for (int a = 0; a < numbers.size();) {
        for (int b = 1; b < numbers.size();) {
            int currentNumber = numbers.get(a);
            if (currentNumber > numbers.get(b)) {

                //Collections.swap(numbers, currentNumber, numbers.get(b));

                numbers.set(numbers.get(a), numbers.get(b));
                numbers.set(numbers.get(b), numbers.get(a));

                a++;
                b++;

            } else if (currentNumber < numbers.get(b)) {
                a++;
                b++;
            }
            System.out.println(numbers);
        }
    }
}
}
Tala
  • 8,888
  • 5
  • 34
  • 38
JP24
  • 207
  • 3
  • 6
  • 13
  • What do you think `numbers.set(numbers.get(a), numbers.get(b));` should be doing? Did you take a look at the `ArrayList` API? – Rohit Jain Sep 01 '13 at 18:42

2 Answers2

3

You are not swapping elements correctly. Instead of

numbers.set(numbers.get(a), numbers.get(b));
numbers.set(numbers.get(b), numbers.get(a));

it should be

int temp = numbers.get(a);
numbers.set(a, numbers.get(b));
numbers.set(b, temp);
Zong
  • 6,160
  • 5
  • 32
  • 46
  • Right so that's now all working, however when I choose to add more elements to the list it sorts i, but at the expense of an indexoutofBounds Exception I know that it's to do with the temporary variable int currentNumber = numbers.get(a); pointing to the next index of the list which doesn't exist. I can't seem to find a way how to get around that problem if you could please help me find a way around it. – JP24 Sep 01 '13 at 19:27
1

The below two statements:

numbers.set(numbers.get(a), numbers.get(b));
numbers.set(numbers.get(b), numbers.get(a));

is not performing swapping. The first argument to the List#set(int, E) method is the index in the list, where you want to set the value passed as 2nd argument. You need to use a temp variable for swapping.

Also, the swapping didn't work for your commented line for the same reason. Collections#swap method take indices for swapping. So, just change:

Collections.swap(numbers, currentNumber, numbers.get(b));

to:

Collections.swap(numbers, a, b);

And please for the love of all that is holy, don't call method from inside a constructor. Remove the method invocation from inside the constructor, and move it to main method like this:

Algorithms algo = new Algorithms();
algo.bubbleSort()
Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525