I am new to parallel programming and I am unsure why the QuickSortParallel method is slower than my sequential version (Without the Parallel.Invoke). I have a jagged array that consists of a hundred thousand 9 digit numbers that I pass to be sorted. Unfortunately, when I use the QuickSortParallel method it ends up being almost 5 times slower than the sequential version.
Could I do more than just using Parallel.Invoke on the data source?
public static void QuickSort_Parallel<T>(T[] array2) where T : IComparable<T>
{
QuickSortParallel(array2, 0, array2.Length - 1);
}
private static void QuickSortParallel<T>(T[] array2, int left, int right)
where T : IComparable<T>
{
if (left >= right)
{
return;
}
SwapElements(array2, left, (left + right) / 2); //median pivot
int last = left;
for (int current = left + 1; current <= right; ++current)
{
//CompareTo, compares current array index value with
if (array2[current].CompareTo(array2[left]) < 0)
{
++last;
SwapElements(array2, last, current);
}
}
SwapElements(array2, left, last);
//Recursive
//Executes each of the provided actions in parallel.
Parallel.Invoke(
() => QuickSortParallel(array2, left, last - 1),
() => QuickSortParallel(array2, last + 1, right)
);
}
static void SwapElements<T>(T[] array2, int i, int j)
{
T temp = array2[i];
array2[i] = array2[j];
array2[j] = temp;
}
QuickSortParallel()sequencially) after it exceed a certain thresold (ex : greater than theNumberOfPhysicalCPU * someconstant) ? - tigrou