title: quicksort using an arraylist java
body: <p>I've written a quicksort for an arraylist and as it stands the logic seems to be sound. The problem I'm having is in the swapping of elements. What seems to be happening is rather than swapping the elements, the method is replacing the existing element with the one to be swapped. an example run start out with a list like [happy, apples, eat, food] and after the sort runs, it comes out [happy, happy, happy, food]. I'm sure my mistake is simple, but I've been staring at it for too long and need fresh eyes on it. Here's my code so far. Thanks in advance!</p>
  
  <pre><code>String pivot = list.get(0); // Choose the first element as the pivot
        int low = first + 1; // Index for forward search
        int high = last; // Index for backward search
  
        while (high &gt; low) 
        { // Search forward from left
            while (low &lt;= high &amp;&amp; list.get(low).compareTo(pivot) &lt;= 0)
            {
  
                low++;
            }
        // Search backward from right
        while (low &lt;= high &amp;&amp; list.get(high).compareTo(pivot) &gt; 0)
        {
            high--;
        }
        // Swap two elements in the list
        if (high &gt; low) 
        {
  
            String temp = list.get(high);
            list.set(high,list.get(low));
            list.set(low,temp);
  
        }
      }
      while (high &gt; first &amp;&amp; list.get(high).compareTo(pivot) &lt;= 0)
      {
  
          high--;
      }
      // Swap pivot with list[high]
      if (list.get(high).compareTo(pivot) &lt; 0) 
      { 
          list.set(first, list.get(high));
          list.set(high,pivot);
          return high;
      }
      else 
      {
          return first;
      }
    }
  </code></pre>