Example #1
0
        private static void mergeSort <T>(T[] src, T[] dest, int low, int high, FastComparator <T> comparator)
        {
            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 && comparator.compare(dest[j - 1], dest[j]); j--)
                    {
                        swap(dest, j, j - 1);
                    }
                }
                return;
            }
            //
            // Recursively sort halves of dest into src
            int destLow  = low;
            int destHigh = high;
            //int mid = (low + high) >>> 1;
            int mid = (int)((uint)(low + high) >> 1);

            mergeSort(dest, src, low, mid, comparator);
            mergeSort(dest, src, mid, high, comparator);
            //
            // 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 (!comparator.compare(src[mid - 1], src[mid]))
            {
                Array.Copy(src, low, dest, destLow, length);
                return;
            }
            //
            // Merge sorted halves (now in src) into dest
            for (int i = destLow, p = low, q = mid; i < destHigh; i++)
            {
                if (q >= high || p < mid && !comparator.compare(src[p], src[q]))
                {
                    dest[i] = src[p++];
                }
                else
                {
                    dest[i] = src[q++];
                }
            }
        }
Example #2
0
 public static void mergeSort <T>(T[] src, T[] dest, int length, FastComparator <T> comparator)
 {
     mergeSort(src, dest, 0, length, comparator);
 }