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<int>.
I am recieving stackoverflow areas in different places depending on the list size.
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.
public void QuickSort()
{
int i, j, lowPos, highPos, pivot;
GenericList<T> leftList = new GenericList<T>();
GenericList<T> rightList = new GenericList<T>();
GenericList<T> tempList = new GenericList<T>();
lowPos = 1; highPos = this.Count;
if (lowPos < highPos)
{
pivot = lowPos;
for (i = 2; i <= highPos; i++)
{
if (this[i].CompareTo(this[pivot]) <= 0)
leftList.Add(this[i]);
else
rightList.Add(this[i]);
}
leftList.Add(this[pivot]);
leftList.QuickSort();
rightList.QuickSort();
for(i=1;i<=leftList.Count;i++)
tempList.Add(leftList[i]);
for(i=1;i<=rightList.Count;i++)
tempList.Add(rightList[i]);
this.items = tempList.items;
this.count = tempList.count;
}
}
Finished Product:
public void QuickSort()
{
Random random = new Random();
int i, j, lowPos, highPos, pivot;
GenericList<T> leftList = new GenericList<T>();
GenericList<T> rightList = new GenericList<T>();
GenericList<T> tempList = new GenericList<T>();
if (this.Count > 1)
{
pivot = random.Next(1,this.Count);
for (i = 1; i <= this.Count; i++)
{
if (i == pivot) continue;
if (this[i].CompareTo(this[pivot]) < 0)
leftList.Add(this[i]);
else
rightList.Add(this[i]);
}
leftList.QuickSort();
rightList.QuickSort();
for(i=1;i<=leftList.Count;i++)
tempList.Add(leftList[i]);
tempList.Add(this[pivot]);
for(i=1;i<=rightList.Count;i++)
tempList.Add(rightList[i]);
this.items = tempList.items;
this.count = tempList.count;
}
}
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 <=), you are setting yourself up for a possible infinite recursion if that pivot ends up in the middle of the sublist.
Example:
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.
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.
See Also: Wikipedia article on Quicksort (with pseudocode)