JayGatsby
JayGatsby

Reputation: 1621

Unable to Run Merge Sort on Java

This is my implementation of Merge Sort, which is not sorting the given array: whenever I try to run it, I get this output:

[I@58c3d9ac

Why am I seeing this? I could not find the problem.

public static int[] mergeSort(int[] arr, int start, int end){
        if (end-start>1){
            int middle = start + (end-start) /2;
            return merges(mergeSort(arr,start,end), mergeSort(arr,start,end));
        }
        else{
            int[]result = new int[end-start];
            for(int i=0; i<end-start; i++)
                result[i] = arr[start+i];
            return result;
        }
    }

    public static int[] merges(int[]arr1,int[]arr2){
    int[] result = new int[arr1.length + arr2.length];

    int outPoint=0;
    int arr1Point=0;
    int arr2Point=0;

    while(outPoint<result.length){
        if(arr1Point >= arr1.length)
            result[outPoint++] = arr2[arr2Point++];
        else if (arr2Point >= arr2.length)
            result[outPoint++] = arr1[arr1Point++];
        else if (arr1[arr1Point] <= arr2[arr2Point])
            result[outPoint++] = arr1[arr1Point++];
        else
            result[outPoint++] = arr2[arr2Point++];
    }
    return result;
    }

}

Upvotes: 0

Views: 92

Answers (2)

Tom
Tom

Reputation: 1273

try printing the result like this:

for (int i : result) {
System.out.println(i);
}

Upvotes: 1

lemonWorld
lemonWorld

Reputation: 221

Show us your main method. That looks like the toString() output of an array. You need to iterate over the contents of the array and print out each element. Or use the method in the comment below.

Upvotes: 0

Related Questions