Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive QuickSort suffering a StackOverflowException
    primarykey
    data
    text
    <p>I am working on a Recursive QuickSort method implementation in a GenericList Class. I will have a second method that accepts a compareDelegate to compare different types, but for development purposes I'm sorting a GenericList&lt;<code>int</code>>.</p> <p>I am recieving stackoverflow areas in different places depending on the list size.</p> <p>I've been staring at and tracing through this code for hours and probably just need a fresh pair of (more experienced) eyes. I definitely want to learn why it is broken, not just how to fix it.</p> <pre><code>public void QuickSort() { int i, j, lowPos, highPos, pivot; GenericList&lt;T&gt; leftList = new GenericList&lt;T&gt;(); GenericList&lt;T&gt; rightList = new GenericList&lt;T&gt;(); GenericList&lt;T&gt; tempList = new GenericList&lt;T&gt;(); lowPos = 1; highPos = this.Count; if (lowPos &lt; highPos) { pivot = lowPos; for (i = 2; i &lt;= highPos; i++) { if (this[i].CompareTo(this[pivot]) &lt;= 0) leftList.Add(this[i]); else rightList.Add(this[i]); } leftList.Add(this[pivot]); leftList.QuickSort(); rightList.QuickSort(); for(i=1;i&lt;=leftList.Count;i++) tempList.Add(leftList[i]); for(i=1;i&lt;=rightList.Count;i++) tempList.Add(rightList[i]); this.items = tempList.items; this.count = tempList.count; } } </code></pre> <p>Finished Product:</p> <pre><code>public void QuickSort() { Random random = new Random(); int i, j, lowPos, highPos, pivot; GenericList&lt;T&gt; leftList = new GenericList&lt;T&gt;(); GenericList&lt;T&gt; rightList = new GenericList&lt;T&gt;(); GenericList&lt;T&gt; tempList = new GenericList&lt;T&gt;(); if (this.Count &gt; 1) { pivot = random.Next(1,this.Count); for (i = 1; i &lt;= this.Count; i++) { if (i == pivot) continue; if (this[i].CompareTo(this[pivot]) &lt; 0) leftList.Add(this[i]); else rightList.Add(this[i]); } leftList.QuickSort(); rightList.QuickSort(); for(i=1;i&lt;=leftList.Count;i++) tempList.Add(leftList[i]); tempList.Add(this[pivot]); for(i=1;i&lt;=rightList.Count;i++) tempList.Add(rightList[i]); this.items = tempList.items; this.count = tempList.count; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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