What's wrong with this QuickSort program? <p>I've written a QuickSort algorithm based off pseudo-code that I had been given. I've been running through the outputs for about 4 hours now and I can't seem to find exactly why my algorithm starts to derail. This sort of collection logic is something I struggle with.</p> <p>Anyway, whenever I run my program there's 3 possible results:</p> <ol> <li>The list is organized and is 100% correct.</li> <li>The list is 90% organized, one or two pairs of elements are off.</li> <li>The list is never sorted and results in an infinite loop.</li> </ol> <p>Given my results it leads me to believe that something has to do with the <code>index</code> variable I'm using. However, I can' figure out why or what's wrong with it.</p> <p>Here's ALL of my code that I use for my QuickSort algorithm:</p> <pre><code>public void QuickSort(IList&lt;int&gt; list, int l, int r) { if (l&gt;=r) return; int index = Partition(list, l, r); Console.WriteLine("index: " + index); QuickSort(list, l, (index-1)); QuickSort(list, (index+1), r); } public int Partition(IList&lt;int&gt; list, int l, int r) { int pivot = list[l]; int i = l; int j = r + 1; do { do { i++; } while(i &lt; list.Count &amp;&amp; list[i] &lt; pivot); do { j--; } while(j &gt; 0 &amp;&amp; list[j] &gt;= pivot); Swap(list, i, j); } while(i&lt;j); Swap(list, i ,j); Swap(list, j, l); return j; } public void Swap(IList&lt;int&gt; list, int i, int j) { //Console.WriteLine("Swapping [i] " + list[i] + " with [j] " + list[j]); //PrintList(list, i, j, false); int temp = list[i]; list[i] = list[j]; list[j] = temp; //PrintList(list, j, i, true); } </code></pre> <p>PrintList is simply used to test my outputs.</p> <p>Here is a sample input/output:</p> <p>INPUT: [18,43,5,73,59,64,6,17,56,63]</p> <p>OUTPUT: [5,6,18,17,43,59,56,63,64,73]</p>