title: Java implementation of Quicksort with Arraylist Troubleshooting
body: <p>I'm trying to implement the quicksort algorithm with ArrayList but can't seem to get this working. It gives me an error for my recursive return call in the sort() function. Please help!</p>
  
  <p>This is the error I get: Exception in thread "main" java.lang.StackOverflowError</p>
  
  <pre><code>    public static void main(String[] args) {
  
      List&lt;Integer&gt; l = new ArrayList&lt;Integer&gt;();
      l.add(10); l.add(7); l.add(13); l.add(22); l.add(9);
      //l.get(2);
  
  
      System.out.println(sort(l));
  
  
  }
  
  
  public static List&lt;Integer&gt; sort(List&lt;Integer&gt; l) {
  
      if (l.size() &lt;= 1) {
          return l;
      }
  
      int pivot = l.get(0);
      List&lt;Integer&gt; left = new ArrayList&lt;Integer&gt;();
      List&lt;Integer&gt; right = new ArrayList&lt;Integer&gt;();
  
      for (int i = 0; i &lt; l.size(); i++) {
          if (l.get(i) &lt; pivot) {
              left.add(l.get(i));
              //System.out.println(left);
          } else {
              right.add(l.get(i));
          }
  
      }
      return join(sort(left), sort(right), pivot);
  }
  
  public static List&lt;Integer&gt; join(List&lt;Integer&gt; left, List&lt;Integer&gt; right, int pivot) {
  
      List&lt;Integer&gt; l =  new ArrayList&lt;Integer&gt;();
  
      for (int i = 0; i &lt; left.size(); i++) {
          l.add(left.get(i));
      }
  
      l.add(pivot);
  
      for (int i = l.size(); i &lt; right.size(); i++) {
          l.add(right.get(i));
      }
      return l;
  }
  </code></pre>
  
  <p>}</p>