0

In the main, whenever I call this method to sort the array, it stops as if it's waiting for a response. Any idea why it's not working?

public void bubbleSort(){
    boolean finished = false;
    boolean swapOccurred = false;
    int i = 0;
    int next = i + 1;

    while (finished == false)
    {

        for (; i < theArray.length - 1; i++)
        {
            if (theArray[i] > theArray[next])
            {
                swapValues(theArray[i], theArray[next]);
                swapOccurred = true;
            }
        }
        if (swapOccurred == false)
        {
            finished = true;
        }
    }
}

private void swapValues(int i, int next) {

    int temp;

    temp = i;
    i = next;
    next = temp;

}
Markopolo
  • 1
  • 1

2 Answers2

2

If it is java, then

  1. The next index is never updated so it stays 1 all the time.
  2. The swap method has no side effect, you want to swap the elements in theArray, but in java methods arguments are passed as values so the next and i variables change the value only inside the swap method, they have nothing to do with the cells of theArray array.
Siddharth
  • 9,349
  • 16
  • 86
  • 148
Zielu
  • 8,312
  • 4
  • 28
  • 41
  • I'm confused,sorry. Do I need to return a value to recognise the change. How do I swap the elements of the array? Thank you – Markopolo Feb 28 '15 at 11:33
  • inside the loop by simple: int tmp = theArray[i]; theArray[i]=theArray[next]; theArray[next] = tmp if you want call the method, then it has to take 3 argu, the array, and the two indexes swap(int[] arr, int i, int j) {in tmp = arr[i], arr[i]= arr[j]; arr[j]=tmp;} – Zielu Feb 28 '15 at 13:22
1

The problem is with your swap.

In C, arguments are passed by value, so when you do the swap, the values being passed in aren't affected, so nothing is happening. You need to pass in pointers to the numbers instead (and then dereference them inside):

private void swapValues(int *i, int *next) {
    int temp;

    temp = *i;
    *i = *next;
    *next = temp;
}

And then call it with the address of the variables to swap:

swapValues(&theArray[i], &theArray[next]);

Edit: Ha - @Zielu saw Java, I saw C. Same problem in both cases, and as @Zielu points out, you also need to increment next. I believe you actually just want to use [i+1] as your index in place of next.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
Dov Rosenberg
  • 673
  • 6
  • 20
  • I actually had i + 1 before the next variable but I was experimenting to see if it worked but substituting with the next variable. No luck – Markopolo Feb 28 '15 at 11:37
  • Next never changes, so you can get rid of it. What you need is i+1: swapValues(theArray[i],theArray[i+1]) - but you still have the problem of the swap doing nothing. Simplest solution in this case is to put the swap code in the for loop. Or, you could put the numbers to swap in an object and pass it to the swap, since objects are passed by reference. See the top answer to [this question](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value), as I think it will help your confusion – Dov Rosenberg Feb 28 '15 at 12:34