-5

I am working on a Merge Sort algorithm but I am having issues getting this pseudo code working properly. I am getting an array out of bounds error on line 100 " s[k] = u[i];"

I am not sure why it is wrong. It is based off the pseduo code. If someone could point me in the right direction it would be much apreciated.

Here is the algorithm:

enter image description here

enter image description here

Here is the code:

import java.util.Arrays;
import java.util.Random;


public class MergeSortAnalysis {
private static int[] aux;


public static void main(String[] args) {
    /**
     * Merge Sort CALL
     */
    System.out.println("_____________________\nMerge Sort\n");
    //random dta
    int data[] =new int[10];
    Random rand = new Random();
    for (int i=0; i<=9;i++) 
    {
        data[i] = rand.nextInt(50) + 1;
    }
    System.out.println("Data Input: ");
    System.out.println(Arrays.toString(data));
    printArray(data);

    System.out.println("Sorted: ");
    MergeSort(10,data);




    /**
     * Merge Sort 2 CALL
     */
    System.out.println("_____________________\nMerge Sort 2\n");
    //random input
    int input[] =new int[10];
    Random rand1 = new Random();
    System.out.println("Input Before Sort: ");
    for (int i=0; i<=9;i++) 
    {
        input[i] = rand1.nextInt(50) + 1;

    }
    printArray(input);

    MergeSortAnalysis.sort(input);
    System.out.println("After Sort: ");
    printArray(input);


}
/**
 * Print Array
 * 
 */
public static void printArray(int p[]) {
    System.out.println("Output");
    System.out.println(Arrays.toString(p));


}




/**
 * Merge Sort Code - does not work
 * 
 * 
 */
public static void MergeSort(int n,int s[]) {
    int h=n/2;
    int m=n-h;
     int[] u = new int[h];
     int[] j = new int[m];
     if(n>1)
     {
      for(int i=0; i<h; i++)
      {
       u[i] = s[i];
      }
      for(int i=0; i<m; i++)
      {
       j[i] = s[h+i];
      }
      MergeSort(h,u);
      MergeSort(m,j);
      merge(h,m,u,j,s);
     }

}

public static void merge(int h, int m, int u[], int v[],int s[]) {
     int i, j, k;
     i=j=k=0;
     while(i<h && j<m) 
     {  
      if(u[i]<v[j])
      {
       s[k] = u[i];
      }
      else
      {
       s[k]=v[j];
       j++;   
      }
      k++;
     }  
     if(i>=h)
     {
      for(int a=j; a<m; a++)
      {
       s[k] = v[a];
       k++;
      }
     }else
     {
      for( int a=i; a<h;a++)
      {
       s[k] = u[a];
       k++;
      } 
     }


    printArray(s);

}
4
  • 1
    Learn how to use a debugger. Commented Nov 9, 2016 at 5:38
  • have you refer this question stackoverflow.com/questions/18549869/… Commented Nov 9, 2016 at 5:39
  • I am familiar with what the error means.- calling for an index out of bound of the set length of the array. But going off the pseudo code I am lost as to why this does not work. This is the 3rd time writing this portion. Commented Nov 9, 2016 at 5:41
  • 1
    If you think that you can copy psuedo-code and have it work exactly like code, you are not thinking correctly. You need to think about how it should be put in Java. Commented Nov 9, 2016 at 5:43

1 Answer 1

0

In merge(), after

   s[k] = u[i];

the code is missing

   i++;

There may be other issues, but this seems to be the main one.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.