Exemple #1
0
        static void Merge(Element[] array, int p, int q, int r)
        {
            if (p + 1 >= r)
                return;
            int i = p;
            int j = q;
            var newarray = new Element[r - p];
            int index = 0;
            while (i < q && j < r)
            {
                int left = array[i].Key;
                int right = array[j].Key;
                if (left <= right)
                {
                    newarray[index++] = array[i];
                    i++;
                }
                else
                {
                    newarray[index++] = array[j];
                    j++;
                }
            }
            if (i >= q)
                while (j < r)
                    newarray[index++] = array[j++];
            else
                while (i < q)
                    newarray[index++] = array[i++];

            index = 0;
            for (i = p; i < r; i++)
                array[i] = newarray[index++];
        }
Exemple #2
0
 static void MergeSort(Element[] array, int p, int r)
 {
     if (p < r - 1)
     {
         int q = (p + r) / 2;
         MergeSort(array, p, q);
         MergeSort(array, q, r);
         Merge(array, p, q, r);
     }
 }