Here is my Bubblesort code:
public static List<int> BubbleSort(List<int> _digitList)
{
List<int> digitList = _digitList;
bool didSwap = true;
while (didSwap)
{
didSwap = false;
for (int i = 0; i < digitList.Count - 1; i++)
{
if (digitList[i] > digitList[i + 1])
{
int temp = digitList[i];
digitList[i] = digitList[i + 1];
digitList[i + 1] = temp;
didSwap = true;
}
}
}
return digitList;
}
And here is my Quicksort Method, which is actually an implementation of the pseudocode on Wikipedia:
public static List<int> Quicksort(List<int> array)
{
if (array.Count <= 1)
{
return array; //An array of Zero ot one elements is already sorted
}
int pivot = 0;
List<int> less = new List<int>();
List<int> greater = new List<int>();
for (int i = 1; i < array.Count;i++ )
{
if (array[i] <= array[pivot])
{
less.Add(array[i]);
}
else
{
greater.Add(array[i]);
}
}
List<int> combined = Quicksort(less);
combined.Add(array[pivot]);
combined.AddRange(Quicksort(greater));
return combined;
}
So for the List = {211, 16, 42, 166, 192, 2, 13, 81, 6, 1, 5, 115, 17, 67};
I get following Stopwatch values.
Bubblesort:
00:00:00.0002873
Quicksort:
00:00:00.0003831
Does this mean my Quicksort code is poor or did I misunderstand the Stopwatch concept?