Results 1 to 2 of 2
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default 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. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Merge sort
    By Aivy in forum New To Java
    Replies: 3
    Last Post: 12-14-2011, 11:29 AM
  2. iterative merge sort
    By baf06 in forum New To Java
    Replies: 5
    Last Post: 10-23-2011, 09:17 PM
  3. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  4. Merge Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:43 PM
  5. Merge Sort Help
    By Hollywood in forum New To Java
    Replies: 5
    Last Post: 01-30-2008, 03:26 AM

Posting Permissions

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