首页 > 代码库 > 夯实基础——归并排序

夯实基础——归并排序

逻辑结构:递归栈

物理结构:数组


归并排序分析:

最优时间复杂度:O(n)

最坏时间复杂度:O(nlog2n

平均时间复杂度:O(nlog2n)

最差空间复杂度:O(n) 

稳定性:稳定


归并排序主要有两个函数:

1 一次归并排序 int MergeArray(int *list1,int list1_size,int *list2,int list2_size);

2  递归归并排序 int MergeSort(int *list,int list_size);

3 非递归归并排序 int MergeSort(int *list,int list_size);


//一次归并排序
int MergeArray(int *list1,int list1_size,int *list2,int list2_size)
{
    int i,j,k,a;
    i=j=k=0;
    int *list=(int *)malloc(sizeof(int)*(list1_size+list2_size));
    while(i<list1_size && j<list2_size)
    {
        if(*(list1+i) <= *(list2+j))
        {
            *(list+k) = *(list1+i);
            i++;
            k++;
        }
        else
        {
            *(list+k) = *(list2+j);
            j++;
            k++;
        }
    }
    while(i < list1_size)
    {
        *(list+k) = *(list1+i);
        i++;
        k++;
    }
    while(j < list2_size)
    {
        *(list+k) = *(list2+j);
        j++;
        k++;
    }
    for(a=0;a<(list1_size+list2_size);a++)
        *(list1+a)=*(list+a);
    free(list);
}

//递归归并排序
int MergeSort(int *list,int list_size)
{
    if(list_size>1)
    {
        int *list1=list;
        int list1_size=list_size/2;
        int *list2=list+list_size/2;
        int list2_size=list_size-list1_size;
        MergeSort(list1,list1_size);
        MergeSort(list2,list2_size);

        MergeArray(list1,list1_size,list2,list2_size);
    }
}

//非递归归并排序
int NonMergeSort(int *list,int list_size)
{
    int i,l_min,l_max,r_min,r_max,cursor;
    int *aux = (int *)malloc(sizeof(int)*list_size);
    for( i = 1 ;i < list_size; i *= 2)
    {
        for(l_min = 0; l_min < list_size - i; l_min = r_max)
        {
                r_min = l_max = l_min + i ;
                r_max = l_max + i;

                if(r_max > list_size)
                    r_max = list_size;

                cursor = 0;

                while(l_min < l_max && r_min < r_max)
                {
                    if(list[l_min] <= list[r_min])
                    {
                        aux[cursor++] = list[l_min++];
                    }
                    else
                    {
                        aux[cursor++] = list[r_min++];
                    }
                }
                while(l_min < l_max)
                    aux[cursor++] = list[l_min++];
                while(r_min < r_max)
                    aux[cursor++] = list[r_min++];
                while( cursor > 0 )
                    list[--r_min] = aux [--cursor];
        }
    }
    free(aux);
}

//非递归归并排序
int NonMergeSort1(int *list , int length)
{

    int i, left_min, left_max, right_min, right_max, next;
    int *tmp = (int*)malloc(sizeof(int) * length);

    if (tmp == NULL)
    {
        fputs("Error: out of memory\n", stderr);
        abort();
    }

    for (i = 1; i < length; i *= 2)
    {
        for (left_min = 0; left_min < length - i; left_min = right_max)
        {
            right_min = left_max = left_min + i;
            right_max = left_max + i;

            if (right_max > length)
                right_max = length;

            next = 0;
            while (left_min < left_max && right_min < right_max)
                tmp[next++] = list[left_min] > list[right_min] ? list[right_min++] : list[left_min++];

            while (left_min < left_max)
                list[--right_min] = list[--left_max];

            while (next > 0)
                list[--right_min] = tmp[--next];
        }
    }

    free(tmp);
}


夯实基础——归并排序