Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your implementation is including the pivot in your sublists. By including the pivot in your sublists (in this case your left list because your condition is &lt;=), you are setting yourself up for a possible infinite recursion if that pivot ends up in the middle of the sublist.</p> <p>Example:</p> <ol> <li>List = [3, 6, <strong>12</strong>, 4, 8] Pivot = 3 (<strong>12</strong>) Left = [3, 6, <strong>12</strong>, 4, 8] Right = [ Empty ]</li> <li>List = [3, 6, <strong>12</strong>, 4, 8] Pivot = 3 (<strong>12</strong>) Left = [3, 6, <strong>12</strong>, 4, 8] Right = [ Empty ]</li> <li>List = [3, 6, <strong>12</strong>, 4, 8] Pivot = 3 (<strong>12</strong>) Left = [3, 6, <strong>12</strong>, 4, 8] Right = [ Empty ]</li> <li>... Infinite Loop</li> </ol> <p>Because the pivot is not excluded (although its final position is known), it can result in you sorting the same list over and over forever, rather than decreasing the size of the lists to sort with each recursive call.</p> <p>If you exclude your pivot (by index, not by value) from the sublists and add it back it into the final tempList between leftList and rightList, it will work properly.</p> <pre><code> ... for (i = 1; i &lt;= highPos; i++) { if (i == pivot) continue; // Add this if (this[i].CompareTo(this[pivot]) &lt;= 0) ... for (i = 1; i &lt;= leftList.Count; i++) tempList.Add(leftList[i]); tempList.Add(this[pivot]); // Add this for (i = 1; i &lt;= rightList.Count; i++) tempList.Add(rightList[i]); ... </code></pre> <p>See Also: <a href="http://en.wikipedia.org/wiki/Quicksort#Algorithm" rel="nofollow noreferrer">Wikipedia article on Quicksort</a> (with pseudocode)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload