C# Quicksort Assignment For my CS minor I was assigned to implement the Quicksort algorithm in C#. I gave it a go and found that I like the implementation of the algorithm more than Mergesort. Now that I've got my Quicksort implementation running, I'm very curious about what optimizations could be done.

using System.Collections.Generic;

namespace Assignment
{
    public class QuickSort
    {
        public void Sort(List&lt;int&gt; list)
        {
            if (list.Count &lt;= 1)
            {
                return;
            }

            int pivot = MedianOfThree(list);
            int equalElementCount = 0;

            List&lt;int&gt; smaller = new List&lt;int&gt;();
            List&lt;int&gt; bigger = new List&lt;int&gt;();

            for (int i = 0; i &lt; list.Count; i++)
            {
                if (list[i] == pivot)
                {
                    equalElementCount++;
                }
                else if (list[i] &lt; pivot)
                {
                    smaller.Add(list[i]);
                }
                else
                {
                    bigger.Add(list[i]);
                }
            }

            Sort(smaller);
            Sort(bigger);

            int pointer = 0;
            for (int i = 0; i &lt; smaller.Count; i++)
            {
                list[pointer++] = smaller[i];
            }
            for (int i = 0; i &lt; equalElementCount; i++)
            {
                list[pointer++] = pivot;
            }
            for (int i = 0; i &lt; bigger.Count; i++)
            {
                list[pointer++] = bigger[i];
            }
        }

        private int MedianOfThree(List&lt;int&gt; list)
        {
            int firstElement = list[0];
            int middleElement = list[list.Count / 2];
            int lastElement = list[list.Count - 1];

            if ((firstElement &gt; middleElement) != (firstElement &gt; lastElement))
            {
                return firstElement;
            }
            else if ((middleElement &gt; firstElement) != (middleElement &gt; lastElement))
            {
                return middleElement;
            }
            else
            {
                return lastElement;
            }
        }
    }
}
Over the years I have seen other students here and they are quite proud of the performance of their code.  In some of those past instances I have discovered that they had simply run their code and thought it was fast.  They did not compare it to other methods. 

You don't mention any testing or provide any numbers.  If you were to do this, just be sure that (1) your code is a Release build and not a Debug, and (2) that you do have disabled "Prefer 32-bit" for Build.

The biggest thing that strikes me about your implementation versus others I have seen is that you make 2 lists (smaller and bigger) per invocation whereas other implementations perform an in-place sort on the list or array.

Does this matter?

YES.  Performance is significantly faster when performing in-place sorting using the one list.

I wrote a sample for you.  I have decide not to post my code here because you are a CS student and your namespace is Assignment which tells me this is classwork.  It would be unethical of me to write this code for you.  What I will do instead you point you to the resources I used, and show my performance timings.

My reference was Wikipedia Quicksort.  In particular I used the Hoare partition scheme.  I translated the psuedocode to C#.

I will share key method signatures.  Yes, plural.

public static void Sort2(List&lt;int&gt; list) =&gt; Sort2(list, 0, list.Count - 1);

private static void Sort2(List&lt;int&gt; list, int lowIndex, int highIndex)
{
    // Good luck on your assignment!
}


I also had a Partition and Swap method.  I leave it to you to decide whether these should be public or private and what they do.

PERFORMANCE


Size of list: 10_000
Number of trials: 10_000
Original QuickSort ElapsedTicks: 117_816_477
In-place QuickSort ElapsedTicks:  50_171_790


Which means your original is 2.3 times slower than my in-place sorting.