-3

The following program is an implementation of quicksort algorithm i found on the internet.

public class QuickSort {

    private int array[];
    private int length;

    public void sortElements(int[] arrayvalues) {

        if (arrayvalues == null || arrayvalues.length == 0) {
            return;
        }
        this.array = arrayvalues;
        length = arrayvalues.length;
        doQuickSort(0, length - 1);
    }

    private void doQuickSort(int lowIndex, int highIndex) {

        int i = lowIndex;
        int j = highIndex;

        int pivot = array[lowIndex + (highIndex - lowIndex) / 2];

        // now Divide the array into two arrays(actually we are maintaining single array only)
        while (i <= j) {

            while (array[i] < pivot) {
                i++;

            }
            while (array[j] > pivot) {
                j--;
            }
            if (i <= j) {
                swapElements(i, j);

                //move index to next position on both sides
                i++;
                j--;

            }
        }

        // call quickSort() method recursively
        if (lowIndex < j) {

            doQuickSort(lowIndex, j);
        }
        if (i < highIndex) {

            doQuickSort(i, highIndex);

        }
    }

    private void swapElements(int i, int j) {

        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;

    }

    public static void main(String a[]) {

        QuickSort quicksort = new QuickSort();
        int[] inputarray = {32, 1, 23, 14, 43, 7, 6, 65};

        System.out.println("Before sorting");
        for (int i : inputarray) {
            System.out.print(i);
            System.out.print(" ");
        }

        quicksort.sortElements(inputarray);

        System.out.println("After sorting");
        for (int i : inputarray) {      //Problem line
            System.out.print(i);
            System.out.print(" ");
        }
    }
}

All is good until the final block, the printing of the array after sorting. The for loop is running on inputarray again. inputarray was defined in main, and then passed to sortElements where it was assigned to the global array defined at the start of the program. All the subsequent manipulations were performed on that global array. So should that be printed in the last for loop? How are the manipulations done on global array being reflected on the inputarray?

Shahid
  • 2,288
  • 1
  • 14
  • 24
Aamir185
  • 9
  • 3
  • Assigning a reference to a variable like `this.array = arrayvalues;` doesn't make a copy of the value - it just makes the two variables point to the same array. You need to actually copy the array (e.g. `Arrays.copyOf(arrayvalues, arrayvalues.length)`) if that's your intent. – Andy Turner Aug 28 '16 at 07:21
  • Note that it's a limiting idea to be assigning the `array` and `length` to variables anyway - this makes the `QuickSort` class non-reentrant (basically, you can't call `sortElements` on the same instance of `QuickSort` from two threads). You can just pass the `array` as a parameter to the methods which need it instead. – Andy Turner Aug 28 '16 at 07:29
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Raedwald Aug 28 '16 at 08:24

1 Answers1

0

When you write

this.array = arrayvalues;

you aren't actually copying arrayvalues: you are just making this.array point to the same array as arrayvalues points to (they're both just references).

As such, when you change the elements of this.array, you're changing the elements of the array pointed to by arrayvalues too, since it's the same object.

If you actually mean to take a copy of arrayvalues, do so: something like:

this.array = Arrays.copyOf(arrayvalues, arrayvalues.length);

Now they point to different arrays, so updates to one aren't reflected in the other.

However, it's not really clear why you'd want that: if you do take a copy, updates done in the QuickSort class aren't reflected in the input array: basically, you can't then see what the sortElements method has changed.

As such, I'd question why you'd not want the code to work in the way it currently does.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • I was under the impression that this.array=arrayvalues; was copying the content of arrayvalues in array and there by creating two distinct arrays. But now that you have mentioned that the above statement does not copy the content but simply makes array refer to arrayvalues, everything makes sense. Thank you for your help buddy. :) – Aamir185 Aug 28 '16 at 07:44