Thread: I"m in the process of writing a Merge Sort program, I'm getting the following errors:

1. Senior Member
Join Date
Nov 2011
Posts
117
Rep Power
0

I"m in the process of writing a Merge Sort program, I'm getting the following errors:

Please consider the following code I have written so far:

Java Code:
public class MergeSort {

public static void main(String[] args) {

int[] A = new int[10];

// Printing the Initial Arrays
System.out.println("Array Elements before Sorting are as follows:");

for(int i = 0;i < A.length;i++){

System.out.println(A[i] + " ");
}

// Call the MergeSort Method here

// Printing Sorted Array here

System.out.println("Sorted Array are as follows:");

//for(int j = 0;j < result.length;i++){

//System.out.println(result[i] + " ");
//}

public static int[] merge_sort(int[] B){

if(B.length <=1){

return B;

}

int midpoint = B.length/2;

int[] left = new int[midpoint];

int[] right;

if(B.length % 2 == 0){

right = new int[midpoint];

}else {
right = new int[midpoint+1];

}

// An Extra Array to store the result

int[] result = new int[B.length];

// Populating the array in the left array

for (int i = 0; i < midpoint; i++){

left[i] = B[i];

}

// Populating in the right array
int x = 0;
for (int j=midpoint;j<B.length;j++){

right[x] = B[j];
x++;
}

// Using recursion to divide the array in left and right again and again

left = merge_sort(left);
right = merge_sort(right);

}// END OF METHOD merge_sort

}// END MAIN METHOD

}// END OF CLASS MergeSort
I'm getting the following error on the following lines in Eclipse: The cross sign indicates
error points in Eclipse:

Java Code:
X public static int[] merge_sort(int[] B){
Error says: Illegal modifier for parameter merge_sort; only final is permitted.

Java Code:
X	return B;
Error Saya: void method cannot return values. I have already defined method return type as an
integer array then why I'm getting this error?

Java Code:
X left = merge_sort(left);
X right = merge_sort(right);
Error Says: The method merge sort is undefined for the type MergeSort

Please let me know what could be the problem.

2. Moderator
Join Date
Apr 2009
Posts
13,212
Rep Power
23

Re: I"m in the process of writing a Merge Sort program, I'm getting the following err

You need to sort your indentation out, as that is all over the place.
It's hard to see where one section begins and ends.

It looks (for example) as if your mergeSort method is declared inside of your main() method.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•