Results 1 to 4 of 4
  1. #1
    laxbro is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default return value problem

    I need to make the mergeSort method return the number of compares and swaps it did but I don't know what I am doing wrong. Still new to java so it's probable simple but I need help. There are some comments from my teacher in their
    Java Code:
    public static int[] mergeSort(int[] arr) {
    		
    		for (int blockSize=1; blockSize<arr.length; blockSize*=2) {
    		
    			for (int leftIndex=0; leftIndex<arr.length; leftIndex+=2*blockSize) {
    				// And make this method also return compares and swaps
    				merge(arr, leftIndex, leftIndex+blockSize, leftIndex+2*blockSize);
    			}
    		}
    		int[] results = new int [3];
    		results[ARRAY_SIZE]=arr.length;
    		results[COMPARES]=compares;
    		results[SWAPS]=swaps;
    		return results;
    	}
    	// Change the return type here, so you can return compares and swaps from it
    	public static int[] merge(int[] sourceArray, int leftStart, int middleIndex, int rightEnd){
    		int compares=0, swaps=0;
    		if (middleIndex>=sourceArray.length) {
    			// In this case, return 0 compares, and 0 swaps
    			int[] results = new int [3];
    			results[COMPARES]=compares;
    			results[SWAPS]=swaps;
    			return results;
    			
    		}
    		if (rightEnd>=sourceArray.length) {
    			rightEnd=sourceArray.length;
    		}
    		int[] tempArray=new int[sourceArray.length];
    		for (int i=leftStart; i<rightEnd; i++) {
    			tempArray[i]=sourceArray[i];
    		}
    		int leftIndex=leftStart;
    		int rightIndex=middleIndex;
    		int currentCopyIndex=leftStart;
    		while(leftIndex<middleIndex && rightIndex<rightEnd) {
    			// Here is your compare
    			compares++;
    			if (tempArray[leftIndex]<tempArray[rightIndex]) {
    				// Here is a swap
    				swaps++;
    				sourceArray[currentCopyIndex]=tempArray[leftIndex];
    				leftIndex++;
    			} else {
    				// Here is another swap
    				swaps++;
    				sourceArray[currentCopyIndex]=tempArray[rightIndex];
    				rightIndex++;			
    			}
    			currentCopyIndex++;
    		}
    		while (leftIndex<middleIndex) {
    			// This too is a swap
    			swaps++;
    			sourceArray[currentCopyIndex]=tempArray[leftIndex];
    			leftIndex++;	
    			currentCopyIndex++;
    		}
    		while (rightIndex<rightEnd) {
    			// And this is a swap
    			swaps++;
    			sourceArray[currentCopyIndex]=tempArray[rightIndex];
    			rightIndex++;	
    			currentCopyIndex++;
    		}
    		int[] results = new int [3];
    		results[COMPARES]=compares;
    		results[SWAPS]=swaps;
    		return results;
    	}
    Last edited by laxbro; 01-19-2013 at 07:26 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: return value problem

    one thing that will make it a LOT easier is if you break your swap and compare statements out into separate methods. That way, you can increment your counter every time the method is called!

  3. #3
    laxbro is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: return value problem

    It has to be done this way for the test program to work

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: return value problem

    No it doesn't.
    Your test program will simply be calling the merge method (presumably).
    Whether that merge method then calls other methods should be irrelevant to it.

    Unless you have been given a very silly test program.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Problem with method & return
    By bigsonny in forum New To Java
    Replies: 32
    Last Post: 03-23-2011, 03:46 AM
  2. no return- problem with actionlistener
    By lparadiso in forum AWT / Swing
    Replies: 2
    Last Post: 11-21-2010, 02:31 AM
  3. Problem with my return method
    By braddy in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 06:34 AM
  4. return array problem
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-30-2010, 05:08 PM
  5. problem while using return statement
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 06:29 PM

Tags for this Thread

Posting Permissions

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