views:

213

answers:

4

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. Definitely wanting 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;
    }

}
+2  A: 

You might get some insight from this series of blogs:

http://reprog.wordpress.com/2010/04/25/writing-correct-code-part-1-invariants-binary-search-part-4a/

http://reprog.wordpress.com/2010/04/27/writing-correct-code-part-2-bound-functions-binary-search-part-4b/

http://reprog.wordpress.com/2010/04/30/writing-correct-code-part-3-preconditions-and-postconditions-binary-search-part-4c/

When you're done with that, trace through your code in your mind with the input data set {1, 2} and tell me what the result will be.

Mark Ransom
STACKOVERFLOW!!! nice, ok. I at least have it working now. I made the pivot the first element of the list, excluded it from being compared to itself and the added it to the left list before calling quicksort again. It sorts, but is it a true QuickSort?
jon
Original Code in Question updated.
jon
@jon, it's a quicksort, but not a typical one. Most quicksorts are in place quicksorts and thus use O(log N) extra memory. Yours will use an average of O(N log N) extra memory.
Justin Peel
+1  A: 

Look what happens when you have a List containing [5,2]. Your pivot will equal 1, so the value used to compare will be 5. The line this[i].CompareTo(this[pivot]) <= 0 will be True, and the number 5 will be placed in leftList. Your next comparison with 2 will also be True, placing the 2 in leftList. Now your leftList will be exactly what it started with: [5,2], which you will call QuickSort with all over again...and it will come out exactly the same ad nausem: StackOverflow.

Nick
that is what @Mark pointed out above ^good call on that though.
jon
+3  A: 

I wouldn't recommend putting the pivot into one of the two groups. If you have just two elements that are equal, you can get an infinite loop. For instance, if you try to sort the array {1,1}, you should get an infinite loop which could be the cause of your stack overflow. Most quicksorts avoid this by swapping the pivot with an element on the edge and then sorting the rest of the array. Another way to handle this would be to put in a line to make sure that you don't add the pivot to the leftList like

if(i != pivot)

Then you'd add the pivot to the tempList in between adding the leftList and adding the rightList.

Edit:

Nick is correct in that you will get the problem for other cases like {5,2}. However, even if you fix the current problem by not putting the pivot into a list that will be sorted again, you might want to make sure that your code can handle repeated elements. An large array of all the same number will give you O(N^2) time complexity, and if N is big enough, then you will get a stack overflow.

Justin Peel
any suggestions on how to reduce the O(NlogN) to O(logN)?
jon
Yes, swap items around in your list instead. Check out this presentation by Sedgewick and Bentley. It has some basic quicksort code in it (in C) as well as addressing one very good way to handle repeated elements. You might want to read up on the section about quicksort in Bentley's Programming Pearls as well.http://www.sorting-algorithms.com/static/QuicksortIsOptimal.pdf
Justin Peel
@jon the best case performance for quicksort is O(n log n), you can't beat that without using a different algorithm.
Joseph Sturtevant
@Joseph, I think he was referring to the extra memory that I mentioned in a different comment. Yes, the average (and best) time complexity for quicksort is O(n log n). However, the memory doesn't have to be O(n log n) as well; it can be O(log n) for a recursive quicksort.
Justin Peel
@Justin Peel Good point. I hadn't considered space complexity.
Joseph Sturtevant
+3  A: 

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:

  1. List = [3, 6, 12, 4, 8] Pivot = 3 (12) Left = [3, 6, 12, 4, 8] Right = [ Empty ]
  2. List = [3, 6, 12, 4, 8] Pivot = 3 (12) Left = [3, 6, 12, 4, 8] Right = [ Empty ]
  3. List = [3, 6, 12, 4, 8] Pivot = 3 (12) Left = [3, 6, 12, 4, 8] Right = [ Empty ]
  4. ... Infinite Loop

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.

        ...
        for (i = 1; i <= highPos; i++)
        {
            if (i == pivot) continue; // Add this
            if (this[i].CompareTo(this[pivot]) <= 0)
        ...
        for (i = 1; i <= leftList.Count; i++)
            tempList.Add(leftList[i]);
        tempList.Add(this[pivot]); // Add this
        for (i = 1; i <= rightList.Count; i++)
            tempList.Add(rightList[i]);
        ...

See Also: Wikipedia article on Quicksort (with pseudocode)

Joseph Sturtevant
but doesn't it need to be placed back in the list being sorted? otherwise I'd have a pivot excluded for every sort called? I could be completely off, what am i missing.
jon
@jon That's exactly what you want. Exclude the pivot because you know where it goes already.
dss539
@jon, the point is that everything in the leftList should be less than or equal to the pivot, and everything in the rightList should be greater than the pivot. There's no reason to include the pivot in the leftList to be sorted again because you already know that its proper place is between leftList and rightList.
Justin Peel
So instead of adding this[pivot] back into the leftList, do a tempList.Add(this[pivot]) in between the for loops building tempList.
jon
@jon That's right. You add the pivot back into tempList between leftList and rightList when you are concatenating them together.
Joseph Sturtevant
ok, did that, in my code I have selected the pivot be the first value in the list, so can I just start the loop with i=2 instead of checking the if == pivot? any concerns there?
jon
There are performance concerns associated with using a fixed element as the pivot in quicksort. In cases where the data is already sorted, for example, a fixed pivot will result in O(n^2) runtime. I would recommend you pick your pivot randomly. That's pretty easy to do in C# using the built in Random class: pivot = myRandom.Next(lowPos, highPos);
Joseph Sturtevant
right on man, thanks for the help.
jon
Added Finished Product to Original Question.
jon