Search code examples
javanested-loopsswapbubble-sort

Is there a difference between these two variable swap methods?


I am trying to swap variables in a array for a Bubble sorting algorithm. Basically, moves the number to the right position if the number on its adjacent right is smaller than itself.

int temp = data[j];
data[j] = data[j+1];   //this swap method
data[j+1] = temp;

int temp = data[j+1];
data[j+1] = data[j];    //OR this swap method?
data[j] = temp;

Both the codes outputs the same result (sorts the array). But just wondering if there are any differences?


Solution

  • No, both are the same. You end up with value A instead of value B and B instead of A.