Results 1 to 3 of 3
  1. #1
    BookOfEliX is offline Member
    Join Date
    Feb 2015
    Posts
    20
    Rep Power
    0

    Default I know the error, how do I go about fixing it.

    Whenever I run the program, I get an error in which it states "java.lang.StackOverflowError" in lines 146,132, and 134. I know that the reason why it is happening, is because there is a method that is calling itself within the same method. Can anyone help me how to solve this issue.

    Java Code:
    import java.util.*;
    public class test{
       public static void main(String[] args){
          int firstSort =(int)(Math.random() * 100);
          int secondSort = (int)(Math.random() * 100);
          int thirdSort = (int)(Math.random() * 100);
          int[] a = new int[1000000];
          int[] b = new int[1000000];
          int[] c = new int[1000000];
          
          for(int i = 1; i <= 1000000; ++i){
            a[i-1] = i;
            //System.out.println(a[i-1]);
          }
          
          for(int i = 1000000 ; i >= 1; i-- ){
            b[i-1] = i;
            //System.out.println(b[i-1]);
          }
          for(int i = 1; i <= 100; ++i){
             c[i-1] = (int)(Math.random() * 1000000);
             //System.out.println(c[i-1]);
          }
          
          //This will check the time for the selection sort method (increasing)
          /*long startTime = System.nanoTime();
          sort(a);
          long endTime= System.nanoTime() - startTime; 
          System.out.println("The running time for the selection sort in increasing order is  + " + endTime/1000 + " Nanoseconds.");*/
          
          //This will check the time for the selection sort method (decreasing)
    /*      long startTime1 = System.nanoTime();
          sort(b);
          long endTime1= System.nanoTime() - startTime1; 
          System.out.println("The running time for the selection sort in decreasing order is + " + endTime1/1000 + " Nanoseconds.");*/
          
          //This will check the time for the selection sort method (random)
         /* long startTime2 = System.nanoTime();
          sort(b);
          long endTime2= System.nanoTime() - startTime2; 
          System.out.println("The running time for the selection sort in decreasing order is + " + endTime2/1000 + " Nanoseconds.");*/
          //This will check the time time for quick sort method (increasing) 
          /*long startTime3 = System.nanoTime();
          partition(a, 0, a.length-1);
          long endTime3= System.nanoTime() - startTime3; 
          System.out.println("The running time for the Quicksort in increasing order is  + " + endTime3/1000 + " Nanoseconds.");
          //This will check the time for quick sort method (decreasing) 
          long startTime4 = System.nanoTime();
          partition(a, 0, a.length-1);
          long endTime4= System.nanoTime() - startTime4; 
          System.out.println("The running time for the Quicksort in decreasing order is  + " + endTime4/1000 + " Nanoseconds.");
          //This will check the time for Quicksort (random)
          long startTime5 = System.nanoTime();
          partition(a, 0, a.length-1);
          long endTime5= System.nanoTime() - startTime5; 
          System.out.println("The running time for the Quicksort in random order is  + " + endTime5/1000 + " Nanoseconds.");
          */
          //this will check the time for Quicksort using median pivot(increasing)
          long startTime6 = System.nanoTime();
          sort3(a, 0, a.length-1);
          long endTime6= System.nanoTime() - startTime6; 
          System.out.println("The running time for the Quicksort in random order is  + " + endTime6/1000 + " Nanoseconds.");
    
          
       }
      //This is the sorting method for Selection Sort
      public static void sort(int[] a){ // Sort a[] into increasing order.
          int N = a.length; // array length
          for (int i = 0; i < N; i++){ // Exchange a[i] with smallest entry in a[i+1...N).
             int min = i; // index of minimal entr.
             for (int j = i+1; j < N; j++)
                if (less(a[j], a[min])) min = j;
                exch(a, i, min);
          }
       }
       private static void exch(int[] a, int i, int j){ 
          int t = a[i]; a[i] = a[j]; a[j] = t; 
       }
       private static boolean less(Comparable v, Comparable w){ 
          return v.compareTo(w) < 0; 
       }
      
      
      //This sorting method will be used for Quicksort
       /*public static void sort1(int[] a){
          StdRandom.shuffle(a); // Eliminate dependence on input.
          sort(a, 0, a.length - 1);
       }*/
       
       //This is the Quicksort Method
       private static void sort(int[] a, int lo, int hi){
          if (hi <= lo) return;
          int j = partition(a, lo, hi); // Partition (see page 291).
          sort(a, lo, j-1); // Sort left part a[lo .. j-1].
          sort(a, j+1, hi); // Sort right part a[j+1 .. hi].
       }
       //The method will be used for partitioning an array so it can be Quicksorted
       private static int partition(int[] a, int lo, int hi){ // Partition into a[lo..i-1], a[i], a[i+1..hi].
          int i = lo, j = hi+1; // left and right scan indices
          Comparable v = a[lo]; // partitioning item
          while (true){ // Scan right, scan left, check for scan complete, and exchange.
             while (less(a[++i], v)) if (i == hi) break;
             while (less(v, a[--j])) if (j == lo) break;
             if (i >= j) break;
                exch(a, i, j);
             }
             exch(a, lo, j); // Put v = a[j] into position
             return j; // with a[lo..j-1] <= a[j] <= a[j+1..hi].
          }
          private static void sort3(int[] a, int lo, int hi){ // See page 289 for public sort() that calls this method.
             if (hi <= lo) return;
             int lt = lo, i = lo+1, gt = hi;
             int v = a[lo];
             while (i <= gt){
                
                if (a[i] < v) exch(a, lt++, i++);
                else if (a[i] > v) exch(a, i, gt--);
                else i++;
             } // Now a[lo..lt-1] < v = a[lt..gt] < a[gt+1..hi].
                sort(a, lo, lt - 1);
                sort(a, gt + 1, hi);
          }
          
       
       }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: I know the error, how do I go about fixing it.

    What you are describing is known as recursion. In recursive methods, there must be some test which ultimately causes the method to return. There are plenty of examples of recursion on the web.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: I know the error, how do I go about fixing it.

    A suggestion for testing: Reduce the size of the arrays. When the code works with a smaller size, increase the size and test again.
    Define a final int to hold the array size and use that variable to define the array sizes.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Help with fixing a error
    By LukasHopkins in forum New To Java
    Replies: 1
    Last Post: 09-06-2012, 02:34 AM
  2. help fixing error in constructor
    By katiebear128 in forum New To Java
    Replies: 3
    Last Post: 10-13-2011, 03:40 AM
  3. [SOLVED] I need help fixing an error
    By leonardjr in forum New To Java
    Replies: 8
    Last Post: 02-21-2009, 08:33 PM
  4. help with fixing my assignment.please...error
    By Jin-enigma-PL in forum New To Java
    Replies: 4
    Last Post: 02-05-2009, 12:12 AM
  5. Exception Error need help fixing
    By skinnybones in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 07:14 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
  •