private static void MergeSort(string[] src, string[] dest,
int low, int high)
{
int length = high - low;
// Insertion sort on smallest arrays
if (length < 7)
{
for (int i=low; i<high; i++)
for (int j=i; j>low && (dest[j-1]).CompareTo(dest[j])>0; j--)
Swap(dest, j, j-1);
return;
}
// Recursively sort halves of dest into src
int mid = (low + high)/2;
MergeSort(dest, src, low, mid);
MergeSort(dest, src, mid, high);
// If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if ((src[mid-1]).CompareTo(src[mid]) <= 0)
{
Array.Copy(src, low, dest, low, length);
return;
}
// Merge sorted halves (now in src) into dest
for(int i = low, p = low, q = mid; i < high; i++)
{
if (q>=high || p<mid && (src[p]).CompareTo(src[q])<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
private static void Swap(string[] x, int a, int b)
{
string t = x[a];
x[a] = x[b];
x[b] = t;
}
}