【分治法】归并分类

归并算法的基本实现:


归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。它的时间复杂度是Θ(nlgn)。归并排序的步骤如下:

1. Divide:把长度为n的输入序列分成两个长度为n/2的子序列。

2. Conquer:对这两个子序列分别采用归并排序。

3. Combine: 将两个排序好的子序列合并成一个最终的排序序列。

在描述归并排序的步骤时又调用了归并排序本身,可见这是一个递归的过程。

所以归并排序的基本的实现代码:

#include <stdio.h>

//merge the two sequential array lists to the temp array ascend

//param:array     the original array

//param:first,mid,last    the index of the original data

//param:temp     the temporary array for the sorting

void MergeArray(int array[], int first, int mid, int last, int temp[])

{

    int i = first;

    int m = mid;

    int j = mid + 1;

    int n = last;

    

    int k = 0;

    

    while(i <= m && j <= n)

    {

        if(a[i] < a[j])

        {

            temp[k++] = a[i++];

        }

        else

        {

            temp[k++] = a[j++];

        }

    }//while

    

    //copy the remainder data to the temp array

    while(i <= m)

    {

        temp[k++] = a[i++];

    }

    while(j <= n)

    {

        temp[k++] = a[j++];

    }

    

    //copy the data from the temp array to the original array.

    for(int i = 0; i < k; i++)

    {

        a[first + i] = temp[i];

    }

    

}//MergeArray()



void MergeSort_Inner(int array[], int first, int last, int temp[])

{

    if(first < last)

    {

        int mid = (first + last) / 2;

        MergeSort(array, first, mid, temp);

        MergeSort(array, mid + 1, last, temp);

        MergeArray(array, first, mid, last, temp);

    }

}//MergeSort_Inner()



bool MergeSort(int array[], int n)

{

    int *p = new int[n];

    if(p == NULL)

    {

        return false;

    }

    MergeSort_Inner(array, 0, n - 1, p);

    delete[] p;

    

    return true;

}//MergeSort()

 

归并排序算法的改进:


从上面的算法中可出,每当集合被分成只含两个元素的子集合时,还需要使用两次递归调用将子集合分成单个元素的集合。这表明算法很多时间不是用在实际的分类而是消耗在处理递归上。为了较少递归调用的深度,只要SMALL(p,q)在输入规模q-p+1适当小的时候,采用能在小规模集合上高效排序的分类算法就能克服底层递归调用所带来的消耗过大的问题。这里选用插入排序,虽然这个算法的时间复杂度为O(n2),但是当n很小(譬如说16)时却能够极快速的工作。

还有一个问题就是,使用了辅助的数组temp[],问题不在于占用了额外的存储空间,问题出在把存放在temp[]中的数据复制到原数组中,是很耗时的。解决的办法就是使用一个整数表示的链接信息数组LINK[]记录原数组中的排序。

你可能感兴趣的:(分治法)