1

My code works only when i am using the swap inside the function of bubble sort,but if i remove the part and put it inside a function and then call it to use as swap .It doesn't works and gives me a wrong output.What is wrong i cant understand

public class BubbleSort
{
  void swap(int a, int b)
  {
    int temp;
    temp = a;
    a = b;
    b = temp;
  }
  
  void sort(int arr[])
  {
    int i, j, n=arr.length;
    
    for(i=0; i<n-1; i++)
    {
      for(j=0; j<(n-i-1); j++)
      {
        if(arr[j] > arr[j+1])
          swap(arr[j], arr[j+1]);
      }
    }
  }
  
  public static void main(String[] args)
  {
    int i;
    int arr[] = {12, 3, 4, 10, 40, 89, 60, 55, 96, 11};
    BubbleSort ob = new BubbleSort();
    ob.sort(arr);
    System.out.println("Array after sorting:");
    for(i=0; i<arr.length; i++)
      System.out.print(arr[i] + " ");
  }
}
Markins
  • 23
  • 5
  • I don't see, why the question was closed (and hidden for non-privileged users). It is not about "reference" vs. "value". It is about swapping two variables. Sure, the question is easy to answer, but it is still a well defined question. – 30thh Sep 28 '21 at 11:17
  • 1
    "It is not about reference vs value" and then you start your answer with "arguments in Java are passed by value, never by reference". It is EXACTLY about reference vs value and the answer why swapping does not work is answered there. – Dalija Prasnikar Sep 28 '21 at 11:58
  • The question is definitely answered by the answer of the referenced thread, but the question itself is not same. Two different questions are allowed to have same answer. This question doesn't [duplicate] the another one. – 30thh Sep 28 '21 at 12:15
  • @30thh For duplicate closure question does not have to be the same. Answers and underlying explanations are those that determine whether the question is duplicate or not. – Dalija Prasnikar Sep 28 '21 at 13:21
  • @Dailija Really? According to this logic the question "Biggest city in Europe" duplicates the question "Capital of Russia" just because the answer of both is "Moscow". Even if you include the explanation and the whole history of the city into the answer, it won't change the fact. Two original questions are different and don't duplicate each other. – 30thh Sep 28 '21 at 13:36
  • @30thh You are overly simplifying now. I explicitly said underlying explanations. That means the cause and effect. Also your answer literally states "arguments in Java are passed by value, never by reference." this question is obvious duplicate, beyond any doubt. Also OP marked the question as a duplicate, so I don't know why are you complaining. – Dalija Prasnikar Sep 28 '21 at 13:50

1 Answers1

1

Method arguments in Java are passed by value, never by reference. Assignments like this make no sense:

void swap(int a, int b)  {
  int temp;
  temp = a;
  a = b;
  b = temp;
}

The caller outside of this method won't see this. For him "a" and "b" will remain their old values.

As a quick workaround you can use AtomicInteger instances to wrap values.

30thh
  • 10,861
  • 6
  • 32
  • 42