-1

[after edit 1] I have seen the other answers here in SO, and from looking at my code, I find that I am adhering to the principle of Java's pass-by-value-of-reference. But still my array is not getting sorted. Please can someone point out any errors I am making in my code?

[after edit 2] Found the problem. It was nothing to do with array passing. In my merge method, it should be if (end-start<=0) not the other way round!

I am trying to implement mergesort. However, I am unable to display values of my array and am unsure of how to pass my arrays in Java such that the original array can be modified.

How can I modify my current code to display the values of the sorted array?

I understand java passes the copy of the array's reference around, but doesnt this mean that the original array gets modified?

CODE:

I am calling mergesort method from my main method.

public static void main(String[] args) {
        int[] qn = {10,22,33,4,5,6,1};
        qn= mergesort(qn,0,qn.length-1);
        for (int i=0;i<qn.length;i++){ //print to see values if sorted/not
               System.out.print(qn[i]+ " ");
        }

    }

public static int[] mergesort(int[] arr,int start,int end){
            int mid = (end+start)/2;
        if (end - start<=0){
            return arr;
        }
            else if (end-start>=1){     
            arr=mergesort(arr,start,mid);
            arr=mergesort(arr,mid+1,end);

        }
            arr=merge(arr,start,end);
            for (int i=0;i<arr.length;i++){
                   System.out.print(arr[i]+ " ");
            }
            System.out.println();
            return arr;
    }

    public static int[] merge(int[] arr,int start,int end){
            int mid= (start+end)/2;
        if(start-end<=0){
            return arr;
        }

        int a= start; int b = mid+1;
        while (a<=mid && b <=end){
            if (arr[b]<arr[a]){
            int tmp = arr[b++];
                for(int i=++mid;i>a;i--){
                arr[i]=arr[i-1];
                }
            arr[a++]=tmp;
                    }
            else if (arr[b]>arr[a]){
            a++;
            }
            else{ //arr[b]=arr[a]
                if(a==mid && b == end){
                break; //all between mid and end will be equal too 
                }
                int tmp= arr[b++];
                            a++;
                for (int i=++mid;i>a;i--){
                arr[i]=arr[i-1];
                }
                arr[a++]=tmp;
                //a++;
                //b++;
            }       

            }
            return arr;
    }
    }

modified my answer based on Manu's below. but it's still not working. Attaching code below:

public static void mergesort(int[] arr,int start,int end){
        int mid = (end+start)/2;
    if (end - start<=0){
        return;
    }
        else if (end-start>=1){     
        mergesort(arr,start,mid);
        mergesort(arr,mid+1,end);

    }
        merge(arr,start,end);
        for (int i=0;i<arr.length;i++){
               System.out.print(arr[i]+ " ");
        }
        System.out.println();
        return;
}

public static void merge(int[] arr,int start,int end){
        int mid= (start+end)/2;
    if(start-end<=0){
        return;
    }

    int a= start; int b = mid+1;
    while (a<=mid && b <=end){
        if (arr[b]<arr[a]){
        int tmp = arr[b++];
            for(int i=++mid;i>a;i--){
            arr[i]=arr[i-1];
            }
        arr[a++]=tmp;
                }
        else if (arr[b]>arr[a]){
        a++;
        }
        else{ //arr[b]=arr[a]
            if(a==mid && b == end){
            break; //all between mid and end will be equal too (pearl)
            }
            int tmp= arr[b++];
                        a++;
            for (int i=++mid;i>a;i--){
            arr[i]=arr[i-1];
            }
            arr[a++]=tmp;
            //a++;
            //b++;
        }       

        }
        return;
}
}
Community
  • 1
  • 1
stretchr
  • 615
  • 2
  • 9
  • 24

1 Answers1

0

You are right, in Java, the copy of the reference of the array gets passed to the function. However, both references point to the same object (the array in your case). This means if you change the values of the array in your merge-method, the values of the array in your mergesort-method changes too. Reqrite your code to this:

 public static void mergesort(int[] arr, ...
 public static void merge(int[] arr, ...

and simply call

 merge(arr, start, mid);
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76