数据结构与算法(C语言版)__归并排序

归并排序是对两个已经排序的数组合并在一起。
下面使用迭代的方式实现归并排序

#define _SCL_SECURE_NO_WARNINGS

#include<iostream>

#include<algorithm>

using namespace std;

template<class T>
void Merge(T *initList, T *mergedList, const int l, const int m, const int n){
    int i1, i2, iResult;
    for (i1 = l, i2 = m + 1, iResult = l; i1 <= m && i2 <= n; iResult++){
        if (initList[i1] <= initList[i2]){
            mergedList[iResult] = initList[i1];
            i1++;
        }
        else{
            mergedList[iResult] = initList[i2];
            i2++;
        }
    }
    copy(initList + i1, initList + m + 1, mergedList + iResult);
    copy(initList + i2, initList + n + 1, mergedList + iResult);

}
int main(){
    int a[] = { 0, 23, 47, 81, 95, 7, 14, 39, 55, 62, 74 };
    int b[11] = { 0 };
    Merge(a, b, 1, 4, 10);
    for (int i = 1; i < 11; ++i){
        cout << b[i] << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}

这里写图片描述

下面看一步一步进行归并排序

#define _SCL_SECURE_NO_WARNINGS

#include<iostream>

#include<algorithm>

using namespace std;

template<class T>
void Merge(T *initList, T *mergedList, const int l, const int m, const int n){
    int i1, i2, iResult;
    for (i1 = l, i2 = m + 1, iResult = l; i1 <= m && i2 <= n; iResult++){
        if (initList[i1] <= initList[i2]){
            mergedList[iResult] = initList[i1];
            i1++;
        }
        else{
            mergedList[iResult] = initList[i2];
            i2++;
        }
    }
    copy(initList + i1, initList + m + 1, mergedList + iResult);
    copy(initList + i2, initList + n + 1, mergedList + iResult);

}

template<class T>
void MergePass(T *initList, T *resultList, const int n, const int s){
    int i;
    for (i = 1; i <= n - 2 * s + 1; i += 2 * s){
        Merge(initList, resultList, i, i + s - 1, i + 2 * s - 1);
    }
    if ((i + s - 1) < n){
        Merge(initList, resultList, i, i + s - 1, n);
    }
    else{
        copy(initList + i, initList + n + 1, resultList + i);
    }
}
int main(){
    /*int a[] = { 0, 23, 47, 81, 95, 7, 14, 39, 55, 62, 74 };
    int b[11] = { 0 };
    Merge(a, b, 1, 4, 10);
    for (int i = 1; i < 11; ++i){
        cout << b[i] << " ";
    }
    cout << endl;*/

    int m[] = { 0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19 };
    int n[11] = { 0 };
    MergePass(m, n, 10, 1);
    for (int i = 1; i < 11; i++)
        cout << n[i] << " ";
    cout << endl;

    MergePass(n, m, 10, 2);
    for (int i = 1; i < 11; i++)
        cout << m[i] << " ";
    cout << endl;

    MergePass(m, n, 10, 4);
    for (int i = 1; i < 11; i++)
        cout << n[i] << " ";
    cout << endl;

    MergePass(n, m, 10, 8);
    for (int i = 1; i < 11; i++)
        cout << m[i] << " ";
    cout << endl;
    system("pause");
    return 0;
}

这里写图片描述

下面看整体的归并排序:

#define _SCL_SECURE_NO_WARNINGS

#include<iostream>

#include<algorithm>

using namespace std;

template<class T>
void Merge(T *initList, T *mergedList, const int l, const int m, const int n){
    int i1, i2, iResult;
    for (i1 = l, i2 = m + 1, iResult = l; i1 <= m && i2 <= n; iResult++){
        if (initList[i1] <= initList[i2]){
            mergedList[iResult] = initList[i1];
            i1++;
        }
        else{
            mergedList[iResult] = initList[i2];
            i2++;
        }
    }
    copy(initList + i1, initList + m + 1, mergedList + iResult);
    copy(initList + i2, initList + n + 1, mergedList + iResult);

}

template<class T>
void MergePass(T *initList, T *resultList, const int n, const int s){
    int i;
    for (i = 1; i <= n - 2 * s + 1; i += 2 * s){
        Merge(initList, resultList, i, i + s - 1, i + 2 * s - 1);
    }
    if ((i + s - 1) < n){
        Merge(initList, resultList, i, i + s - 1, n);
    }
    else{
        copy(initList + i, initList + n + 1, resultList + i);
    }
}

template<class T>
void MergeSort(T *a, const int n){
    T *tempList = new int[n + 1];
    for (int l = 1; l < n; l *= 2){
        MergePass(a, tempList, n, l);
        l *= 2;
        MergePass(tempList, a, n, l);
    }
    delete[]tempList;//删除临时的数组,防止内存泄露。
}
int main(){
    /*int a[] = { 0, 23, 47, 81, 95, 7, 14, 39, 55, 62, 74 };
    int b[11] = { 0 };
    Merge(a, b, 1, 4, 10);
    for (int i = 1; i < 11; ++i){
        cout << b[i] << " ";
    }
    cout << endl;*/

    int m[] = { 0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19 };
    int n[11] = { 0 };
    MergePass(m, n, 10, 1);
    for (int i = 1; i < 11; i++)
        cout << n[i] << " ";
    cout << endl;

    MergePass(n, m, 10, 2);
    for (int i = 1; i < 11; i++)
        cout << m[i] << " ";
    cout << endl;

    MergePass(m, n, 10, 4);
    for (int i = 1; i < 11; i++)
        cout << n[i] << " ";
    cout << endl;

    MergePass(n, m, 10, 8);
    for (int i = 1; i < 11; i++)
        cout << m[i] << " ";
    cout << endl;

    cout << "上面是中间数据测试" << endl;
    cout << "下面是MergeSort...:" << endl;

    int x[] = { 0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19 };
    MergeSort(x, 10);
    for (int i = 1; i < 11; i++)
        cout << x[i] << " ";
    cout << endl;
    system("pause");
    return 0;
}

这里写图片描述

总结:归并排序的速度和快速排序一样,使用三个函数写成一个归并排序,唯一的缺点是需要使用两倍的内存, 如果要排序1M的数据,就要使用2M的内存,因为使用了临时数组。但是现在电脑内存都比较大,所以这个问题也不算严重。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值