title: Quick Sort stackoverflow error for large arrays
body: <p>So I was assigned to implement a quick sort algorithm and compare the running times for arrays with size 500, 3500, and 80000. The arrays are populated with random numbers:</p>
  <blockquote>
  <p>(int)Math.random()*1000000</p>
  </blockquote>
  <p>My quicksort algorithm works for the arrays with size 500 and 3500, but I always get a stackoverflow error when I try to sort the third array with size 80000. My other sorting algorithms handle these arrays just fine.</p>
  <p>My quick sort method:</p>
  <pre><code>public static void quickSort(int[] a, int p, int r)
  {
      if(p&lt;r)
      {
          int q=partition(a,p,r);
          quickSort(a,p,q);
          quickSort(a,q+1,r);
      }
  }
  </code></pre>
  <p>My partition method:</p>
  <pre><code>private static int partition(int[] a, int p, int r) {
  
      int x = a[p];
      int i = p;
      int j = r;
  
      while (true) {
          do {
              i++;
          } while (i &lt; r &amp;&amp; a[i] &lt; x);
          do {
              j--;
          } while (j &gt; p &amp;&amp; a[j] &gt; x);
  
          if (i &lt; j) {
              int tmp = a[i];
              a[i++] = a[j];
              a[j--] = tmp;
          } else {
              return j;
          }
      }
  }
  </code></pre>
  <p>I read that I could simply change my stack size in the VM options (not sure how to do that) but that is just ignoring the problem in my algorithm. What's causing the error? Thanks!</p>
  <p>My driver class:</p>
  <pre><code>public class Driver {
  
      public static void main(String[] args) {
  
          int[] array1 = new int[500];
          int[] array2 = new int[3500];
          int[] array3 = new int[80000];
  
          for(int i=0; i&lt;array1.length; i++) {
              array1[i]=(int)(Math.random()*100000);
          }
  
          for(int i=0; i&lt;array2.length; i++) {
              array2[i]=(int)(Math.random()*100000);
          }
  
          for(int i=0; i&lt;array3.length; i++) {
              array3[i]=(int)(Math.random()*100000);
          }
  
          //~~~~~~~~~~~INSERTION~~~~~~~~~~~~~~~//
  
          System.out.println(&quot;INSERTION SORT:\n_______________&quot;);
          System.out.println(&quot;500 Elements: &quot;+SortTimes.runTime(SortTimes.INSERTION,array1)+&quot; ms&quot;);
          System.out.println(&quot;3500 Elements: &quot;+SortTimes.runTime(SortTimes.INSERTION,array2)+&quot; ms&quot;);
          System.out.println(&quot;80000 Elements: &quot;+SortTimes.runTime(SortTimes.INSERTION,array3)+&quot; ms&quot;);
  
          //~~~~~~~~~~~BUBBLE~~~~~~~~~~~~~~~//
  
          System.out.println(&quot;\n\nBUBBLE SORT:\n_______________&quot;);
          System.out.println(&quot;500 Elements: &quot;+SortTimes.runTime(SortTimes.BUBBLE,array1)+&quot; ms&quot;);
          System.out.println(&quot;3500 Elements: &quot;+SortTimes.runTime(SortTimes.BUBBLE,array2)+&quot; ms&quot;);
          System.out.println(&quot;80000 Elements: &quot;+SortTimes.runTime(SortTimes.BUBBLE,array3)+&quot; ms&quot;);
  
          //~~~~~~~~~~~MERGE~~~~~~~~~~~~~~~//
  
          System.out.println(&quot;\n\nMERGE SORT:\n_______________&quot;);
          System.out.println(&quot;500 Elements: &quot;+SortTimes.runTime(SortTimes.MERGE,array1)+&quot; ms&quot;);
          System.out.println(&quot;3500 Elements: &quot;+SortTimes.runTime(SortTimes.MERGE,array2)+&quot; ms&quot;);
          System.out.println(&quot;80000 Elements: &quot;+SortTimes.runTime(SortTimes.MERGE,array3)+&quot; ms&quot;);
  
          //~~~~~~~~~~~QUICK~~~~~~~~~~~~~~~//
  
          System.out.println(&quot;\n\nQUICK SORT:\n_______________&quot;);
          System.out.println(&quot;500 Elements: &quot;+SortTimes.runTime(SortTimes.QUICK,array1)+&quot; ms&quot;);
          System.out.println(&quot;3500 Elements: &quot;+SortTimes.runTime(SortTimes.QUICK,array2)+&quot; ms&quot;);
          System.out.println(&quot;80000 Elements: &quot;+SortTimes.runTime(SortTimes.QUICK,array3)+&quot; ms&quot;);
      }
  }
  </code></pre>
  <p>Here is my SortTimes class:</p>
  <pre><code>public class SortTimes {
  
      public final static int MERGE = 1;
      public final static int QUICK = 2;
      public final static int BUBBLE = 3;
      public final static int INSERTION = 4;
  
      public static double runTime(int sortMethod, int[] array) {
  
          double startTime;
          double endTime;
  
          switch(sortMethod) {
              case MERGE:
                  startTime = System.currentTimeMillis();
                  lab12.mergeSort(array);
                  endTime = System.currentTimeMillis();
                  break;
  
              case QUICK:
                  startTime = System.currentTimeMillis();
                  lab12.quickSort(array, 0, array.length-1);
                  endTime = System.currentTimeMillis();
                  break;
  
              case BUBBLE:
                  startTime = System.currentTimeMillis();
                  lab12.bubbleSort(array);
                  endTime = System.currentTimeMillis();
                  break;
  
              case INSERTION:
                  startTime = System.currentTimeMillis();
                  lab12.insertionSort(array);
                  endTime = System.currentTimeMillis();
                  break;
  
              default:
                  startTime = -1;
                  endTime = 0;
                  break;
          }
  
          return endTime-startTime;
      }
  }
  </code></pre>