java - Quick Sort - ArrayList - is not working
<p>I am having trouble finding a bug in this sorting algorithm. When I pass an array to the sort method, say 5,6,4,7,1, I get the same array as result. I've been going through code but I cannot find where it is going wrong. Disregard SortThread task, it's an argument that will update the progress bar, but I am not using it right now.</p>

<pre><code>class QuickSort extends Sort {

@Override
ArrayList&lt;Integer&gt; sort(ArrayList&lt;Integer&gt; array, SortThread task) {

    if (array.size() &lt;= 1) {
        return array;
    }

    int middle = (int) Math.ceil((double) array.size() / 2);
    int pivot = array.get(middle);

    ArrayList&lt;Integer&gt; less = new ArrayList&lt;&gt;();
    ArrayList&lt;Integer&gt; greater = new ArrayList&lt;&gt;();

    for (int i = 0; i &lt; array.size(); i++) {
        if (array.get(i) &lt;= pivot) {
            if (i == middle) {
                continue;
            }
            less.add(array.get(i));
        } else {
            greater.add(array.get(i));
        }
    }

    return concatenate(sort(less, task), pivot, sort(greater, task));
}

private ArrayList&lt;Integer&gt; concatenate(ArrayList&lt;Integer&gt; less, int pivot, ArrayList&lt;Integer&gt; greater) {

    ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();

    for (int i = 0; i &lt; less.size(); i++) {
        list.add(less.get(i));
    }

    list.add(pivot);

    for (int i = 0; i &lt; greater.size(); i++) {
        list.add(greater.get(i));
    }

    return list;
}
</code></pre>

<p>}</p>
