1

So I have a quick_sort method that I am testing out like this:

public static void main(String[] args){
  int x[] = {5,4,9,6,3,4,11,99,3,4,5,2,4,7,8,34,5,34543,545,454};
  quick_sort(x,0,x.length-1);
  System.out.println(Arrays.toString(x));
  System.out.println(Arrays.toString(quick_sort(x,0,x.length-1)));
}

The output of this method is:

[5, 4, 9, 6, 3, 4, 11, 99, 3,4 ,5 ,2, 4, 7, 8, 34, 5, 34543, 545, 454]

[2, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 7, 8, 9, 11, 34, 99, 454, 545, 34543]

My question is, I thought java was pass-by-value and that arrays were Objects. So i don't see why both of my outputs are not sorted. Why is only the second one, where I return array, printing out as sorted? When i pass x into the function shouldn't i just receive a copy of that pointer that still points to the same object in the heap? So any changes i make to that array should be modified for the original pointer.

edit:

I figured out that it has something to do with the fact that x is a literal. If i declare x as a new int[] in fill it manually, then both outputs seem to work fine.

Here is my quicksort:

 public static int[] quick_sort(int arr[], int left, int right) {
if(left==right)
    return arr;
int holdLeft = left;
int holdRight = right;
int partition = left + (right-left)/2;
while(left<right){
    while(arr[left] < arr[partition])
    left++;
    while(arr[right]>arr[partition])
    right--;
    if(left <= right){
    swap(arr,left,right);
    left++;
    right--;
    }
}
if (holdLeft < right)
quick_sort(arr,holdLeft, right);
if (left < holdRight)
quick_sort(arr,left, holdRight);

return arr;
}
user3016349
  • 67
  • 1
  • 1
  • 5
  • Please post your quicksort code – The Archetypal Paul Nov 16 '14 at 22:11
  • I can't get your question, You mean if java is pass-by-value it should change globally the value of object? Well, I used to believe that is the definition of pass-by-reference. – muradin Nov 16 '14 at 22:32
  • 1
    @muradin java is pass-by-value but it passes the pointer that points to the array by value, not the object itself. so that is why it is technically pass-by-value, but in effect basically pass by reference – user3016349 Nov 16 '14 at 22:34
  • I cannot reproduce. I get `[2, 3, 4, 4, 3, 4, 6, 4, 5, 5, 7, 5, 9, 8, 11, 34, 99, 454, 545, 34543]` both times. – Ole V.V. Apr 10 '22 at 15:14

3 Answers3

2

Your quick_sort() doesn't return an array.

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references and those references are passed by value. (Is Java "pass-by-reference" or "pass-by-value"?)

Community
  • 1
  • 1
apxcode
  • 7,696
  • 7
  • 30
  • 41
  • I understand how objects are passed. I still don't get why the original array x isnt being modified in the quick sort? if x points to an array, and i modify that array, why isnt it being modified? – user3016349 Nov 16 '14 at 22:03
1

The problem is that in your quick_sort() method you are creating another array and you are storing your changes int that new array, and returning that new array instead of array x or the reference to x !!!

Muhammad
  • 6,725
  • 5
  • 47
  • 54
0

You haven't returned the quick sorted array as anything.

If you had declared int y[] = quick_sort(x, 0, x.length-1); then it would work because you have assigned the sorted array to something. At the moment you aren't declaring it as anything.

Moiety Design
  • 473
  • 4
  • 9
  • My question is basically why do i need to set it to anything? Why isnt the array that i passed in being modified instead? since it is an object, shouldnt it be modified within the function? – user3016349 Nov 16 '14 at 22:17