0

The program works as I expected if I put these lines of code:

temp = arr[x];
arr[x] = arr[y];
arr[y] = temp; 

under the selectionSort function, but not with the swap function.

Here's my code:

class selectionSort {
 public static void printArray(int[] arr) {
  for (int x = 0; x < arr.length; x++) {
   System.out.print("[" + arr[x] + "],");
  }
  System.out.println();
 }

 public static void selectionSort(int[] arr) {
  for (int x = 0; x < arr.length - 1; x++) {
   for (int y = x + 1; y < arr.length; y++) {
    if (arr[x] > arr[y]) {
     swap(arr[x], arr[y]); // this is the line that doesn't work
     //  int temp = arr[x];
     //  arr[x] = arr[y];
     //  arr[y] = temp;  
    }
   }
  }
  System.out.println();
 }

 public static void swap(int x, int y) {
  int temp = x;
  x = y;
  y = temp;
 }

 public static void main(String[] args) {
  int[] arr = new int[] {
   32,
   213,
   432,
   21,
   2,
   5,
   6
  };

  printArray(arr);
  selectionSort(arr);
  printArray(arr);
 }
}

Can anyone explain why, or give me some hints?

Thank you!

grooveplex
  • 2,492
  • 4
  • 28
  • 30

3 Answers3

0

When you are calling swap(arr[x],arr[y]) inside selection sort() then it will wont work because you are calling the function by value not by the reference. Therefore inside swap(int x, int y), the values are swapping but it is not reflecting in the array. When you are putting the lines in the selection sort() then it will work because arr[x] and arr[y] within the scope only. Please go through the below links for better clarity :

http://cs-fundamentals.com/tech-interview/c/difference-between-call-by-value-and-call-by-reference-in-c.php

https://javapapers.com/core-java/java-pass-by-value-and-pass-by-reference/

Bishal Dubey
  • 150
  • 7
0

Everything in Java is passed by value, including references to arrays. You need to pass the int[] array to the swap method so that the swap method can modify the array properly, like so:

class selectionSort{
    public static void printArray(int[] arr){
        for(int x = 0; x < arr.length; x++){
             System.out.print("[" + arr[x] + "],");
        }
        System.out.println();
    }

    public static void selectionSort(int[] arr){
        for(int x =0; x < arr.length-1; x++){
            for(int y = x + 1; y < arr.length; y++){
                if(arr[x] > arr[y]){
                    swap(arr[x], arr[y], arr);
                }
            }
        }
        System.out.println();
    }

    public static void swap(int x, int y, int[] arr){
        int temp = arr[x];
        arr[x] = arr[y];
        arr[y] = temp;
    }

    public static void main(String[] args){
        int[] arr = new int[]{32,213,432,21,2,5,6}; 

        printArray(arr);
        selectionSort(arr);
        printArray(arr);
    }
}

Check out this duplicate answer (3rd answer down): Jave method to swap primitives

Brandon
  • 933
  • 1
  • 10
  • 19
0

Java does not send variable references. It makes a copy of the value and that is why the original values are not changed. Thus, you need to return the swapped values from the swap function.

RisingSun
  • 1,693
  • 27
  • 45