1 2 Previous Next 20 Replies Latest reply: Oct 22, 2008 3:07 PM by 793982 RSS

    Recursive mergeSort... small problem with infinate recursion.

    807589
      Basically, I have made a recursive mergeSort method, but there is a small problem...im getting stuck with an eternal recursion, which is obviously followed up swiftly by a stack flow error, problem is I cant see where im going wrong...

      The error reads, "Exception in thread "main" java.lang.StackOverflowError at part3.mergeSort(part3.java:56)"

      Heres the code... any help is highly appreciated, as I am new to java, and this is for university.
      public class part3
      {      
         
          public static void main(String[] args) 
          {
            // Declare array of unsorted values.
            int[] values = {1,3,5,7,9,2,4,6,8,10};
            
            // Display unsorted values on screen.
            System.out.println("Unsorted Values");
            for(int i= 0;i < values.length; i++)
            {
                 System.out.println(values);
      }

      mergeSort(values, values.length);

      output(values);



      }
           
      public static void mergeSort(int[] vals, int n)
      {
      // Declare necessary values

      int lowCount, highCount;
      int[] low, high;
      int i, j, k;

      if (n > 1)
      {
                low = new int[n];
                high = new int[n];
                
                for(i=0,j=0,k=0; i < n; i++)
                {
                     if(vals[i] < low.length)
                          
                     {
                          low[j] = vals[i];
                          j++;
                     }
                     
                     else
                          
                     {
                          high[k] = vals[i];
                          k++;
                     }
                     
                lowCount = j;
                highCount= k;
                
                mergeSort(low, lowCount);
                mergeSort(high, highCount);
                
                for(i = 0; i < lowCount; i++)
                {
                     vals[i] = low[i];
                }
                
                for(i = 0; i < highCount; i++)
                {
                     vals[i+lowCount] = high[i];
                }
                
                     
                }
      }
      }

      public static void output(int[] data)
      {
      for(int i = 0; i < data.length; i++)
      {
           System.out.print(data[i] + " ");
                System.out.println();
      }
      }     
      }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
        1 2 Previous Next