《数据结构:邓俊辉版》——并归排序

瘦欲@ 提交于 2019-12-03 09:31:51

 

void MergeSort(int low, int high)
{
    int mid = (low + high) / 2;
    if (high - low < 1)
    {
        return;
    }
    MergeSort(low, mid);
    MergeSort(mid + 1, high);
    Merge(low, mid, high);
}

void Merge(int low, int mid, int high)
{
    int* A = g_szArray + low;
    int llen = mid - low + 1;
    int* B = new int[llen];
    for (int i = 0; i < llen; i++)
    {
        B[i] = g_szArray[low + i];
    }

    int hlen = high - mid;
    int* C = g_szArray + mid + 1;
    for (int i = 0, j = 0, k = 0; (j < llen) || (k < hlen);)
    {
        if ((j < llen) && (!(k < hlen) || (B[j] <= C[k])))
        {
            A[i++] = B[j++];
        }
        if ((k < hlen) && (!(j < llen) || (C[k] < B[j])))
        {
            A[i++] = C[k++];
        }
    }

    delete []B;
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!