void OnceSort(int *a, int size);
void MergeSort(int *a, int size)
{
    if (size <= 1)
        return ;

    int which = 2;
    int left = size/which;

    MergeSort(a, left);
    MergeSort(a + left, size - left);
    OnceSort(a, size);
}


void OnceSort(int *a, int size)
{
    int len1 = size/2;
    int len2 = size-len1;
    int *L = new int[len1];
    int *R = new int[len2];

    int j = 0;
    int k = 0;
    int i = 0;
    
    for (i = 0; i < len1; i++)
    {
        L[i] = a[i];
    }

    for (i = len1; i < size; i++)
    {
        R[i-len1] = a[i];
    }

    for ( i = 0, j = 0, k = 0; k < size; k++)
    {
        if( i >= len1 || (j < len2 && L[i] > R[j] ))
        {
            a[k] = R[j];
            j++;
        }
        else
        {
            a[k] = L[i];
            i++;
        }
    }

    delete []L;
    delete []R;
}

 

posted on 2016-11-05 23:58  听风的日子  阅读(169)  评论(0编辑  收藏  举报