Results 1 to 6 of 6
  1. #1
    desconocido is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default help with mergesort

    hi,
    im trying to do a mergesort algorithm with random numbers
    i know i need to divide the array into other 2 until i got ordered arrays(or 1 and 0 length arrays) that part works great
    but when i try to order (with a length of 2 or more) theres almost always an error of array out of bounds, maybe i forgot something?
    heres my meth

    Java Code:
    public static int[] order(int b[], int c[]){
    		int a[]=new int[(b.length)+(c.length)];
    		int i=0;
    		int j=0;
    		for(int k=0;k<((b.length)+(c.length));k++){
    			if((b[i]<=c[j] && i !=b.length && j !=c.length) || (i !=b.length && j ==c.length)){
    				a[k]=b[i];
    				i=i+1;
    			}if((b[i]>c[j] && i !=b.length && j !=c.length) || (i ==b.length && j !=c.length)){
    				a[k]=c[j];
    				j=j+1;
    			}
    		}
    		return a;
    	}
    please help!
    Last edited by desconocido; 09-15-2010 at 02:29 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    error of array out of bounds
    What array, what variable, what line of code and what is the index's value.

  3. #3
    desconocido is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    it says line 10,but thats
    Java Code:
    if((b[i]<=c[j] && i !=b.length && j !=c.length) || (i !=b.length && j ==c.length)){
    thats why i was thinking if there is other case i didnt write

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    That's one part where are the other three:
    What array, what variable, what line of code and what is the index's value.

    Interesting placement of } and if. Was that intentional?

    }if((b[i]>c[j]
    Last edited by Norm; 09-15-2010 at 03:38 AM.

  5. #5
    desconocido is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    i dont know witch array/variable may be
    and index value is variable

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    Add some print outs to show the values of the variables.
    The error message will show the index value that is out of range.

Similar Threads

  1. mergeSort
    By Blue_beaver in forum New To Java
    Replies: 2
    Last Post: 10-11-2009, 10:55 PM

Posting Permissions

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