2011年软件设计师辅导:并行排序算法(4)

2011年软件设计师辅导:并行排序算法(4) #
// Merge sort #
MergeSort < T > mergeSort = new MergeSort < T > ();
List < T[] > source = new List < T[] > (processorCount); #
foreach (ParallelEntity pe in partArray)
#
{
#
source.Add(pe.Array); #
} #
mergeSort.Sort(array, source, comparer); #
} #
} #
} #
多路归并排序类 MergeSort
using System; #
using System.Collections.Generic;
#
using System.Linq; #
using System.Text; #
namespace Sort #
{
#
/**/ /// #
/// MergeSort
#
/// #
/// #
public class MergeSort < T > #
{
#
public void Sort(T[] destArray, List < T[] > source, IComparer < T > comparer)
{ #
// Merge Sort #
int [] mergePoint = new int [source.Count]; #
for ( int i = 0 ; i < source.Count; i ++ )
#
{
mergePoint[i] = 0 ;
} #
int index = 0 ;
#
while (index < destArray.Length) #
{
int min = - 1 ; #
for ( int i = 0 ; i < source.Count; i ++ ) #
{
#
if (mergePoint[i] >= source[i].Length) #
{ #
continue ;
#
} #
if (min < 0 )
#
{ #
min = i;
#
} #
else
#
{ #
if (comparer.Compare(source[i][mergePoint[i]], source[min][mergePoint[min]]) < 0 )
{
#
min = i; #
}
#
} #
}
if (min < 0 )
{
#
continue ; #
} #
destArray[index ++ ] = source[min][mergePoint[min]]; #
mergePoint[min] ++ ; #
}
}
#
} #
} #
#