排序算法之归并排序

1、迭代归并排序

将两个数当做已经排好序,再进行归并排序;然后再将上面两个排好序的数据进行归并排序;依次类推,最后将所有数据归并排序。如下图所示:
这里写图片描述


2、归并排序demo

#include <iostream>

using namespace std;

template <class T>
void merge(T *initList, T *mergeList, int f, int s, int l);

template <class T>
void mergePass(T *initList, T *mergeList, const int n, const int s);

template <class T>
void mergeSort(T *initList, const int n);

int main(int argc, const char *argv[])
{
    /*
    cout << "初始数据:" << endl;
    int a[] = {0, 1, 14, 45, 56, 16, 27, 46, 57, 78, 88};
    int b[11] = {0};
    for (int i = 1; i < 11; i++){
        cout << a[i] << " ";
    }
    cout << endl;

    cout << "开始测试merge..." << endl;
    merge(a, b, 1, 4, 10);
    for (int i = 1; i < 11; i++){
        cout << b[i] << " ";
    }

    cout << endl;
    cout << endl;

    cout << "初始数据:" << endl;
    int x[] = {0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19};
    int y[11];
    for (int i = 1; i < 11; i++){
        cout << x[i] << " ";
    }
    cout << endl;

    cout << "开始测试mergePass..." << endl;
    mergePass(x, y, 10, 1);
    for (int i = 1; i < 11; i++){
        cout << y[i] << " ";
    }
    cout << endl;

    mergePass(y, x, 10, 2);
    for (int i = 1; i < 11; i++){
        cout << x[i] << " ";
    }
    cout << endl;

    mergePass(x, y, 10, 4);
    for (int i = 1; i < 11; i++){
        cout << y[i] << " ";
    }
    cout << endl;

    mergePass(y, x, 10, 8);
    for (int i = 1; i < 11; i++){
        cout << x[i] << " ";
    }

    cout << endl;
    cout << endl;
*/
    cout << "初始数据:" << endl;
    int data[] = {0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19}; //注意:data[0],只是为了变成方便,不作为排序数据
    for (int i = 1; i < 11; i++){
        cout << data[i] << " ";
    }
    cout << endl;

    cout << "开始测试mergeSort..." << endl;
    mergeSort(data, 10);
    for (int i = 1; i < 11; i++){
        cout << data[i] << " ";
    }
    cout << endl;

    return 0;
}

template <class T>
void merge(T *initList, T *mergeList, int f, int s, int l)
{
    int i, j, iResult;
    for (i = f, j = s + 1, iResult = f; i <= s && j <= l; iResult++){
        if (initList[i] < initList[j]){
            mergeList[iResult] = initList[i];
            i++;
        }
        else {
            mergeList[iResult] = initList[j];
            j++;
        }
    }

    copy(initList+i, initList+s+1, mergeList+iResult);
    copy(initList+j, initList+l+1, mergeList+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 *initList, const int n)
{
    T *tempList = new int[n+1];
    for (int i = 1; i < n; i *= 2){
        mergePass(initList, tempList, n, i);
        i *= 2;
        mergePass(tempList, initList, n, i);
    }

    delete[] tempList;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
归并排序(Merge Sort)是一种稳定的、基于比较的排序算法,最坏时间复杂度为 O(nlogn)。其基本思想是将待排序序列分成若干个子序列,每个子序列都是有序的,然后将子序列合并成整体有序的序列。 归并排序的实现方法有两种:自顶向下和自底向上。 自顶向下的归并排序算法实现: 1. 将待排序序列分成两个子序列,分别对这两个子序列进行递归排序。 2. 将两个已经排好序的子序列合并为一个有序序列。 自底向上的归并排序算法实现: 1. 将待排序序列每个元素看成一个独立的有序序列,进行两两合并。 2. 得到 n/2 个长度为 2 的有序序列,再两两合并。 3. 重复步骤 2,直到得到一个长度为 n 的有序序列。 下面是自顶向下的归并排序算法的实现代码(使用了递归): ``` void MergeSort(int arr[], int left, int right) { if (left >= right) return; int mid = left + (right - left) / 2; MergeSort(arr, left, mid); MergeSort(arr, mid + 1, right); int* temp = new int[right - left + 1]; int i = left, j = mid + 1, k = 0; while (i <= mid && j <= right) { if (arr[i] <= arr[j]) temp[k++] = arr[i++]; else temp[k++] = arr[j++]; } while (i <= mid) temp[k++] = arr[i++]; while (j <= right) temp[k++] = arr[j++]; for (int p = 0; p < k; p++) arr[left + p] = temp[p]; delete[] temp; } ``` 下面是自底向上的归并排序算法的实现代码(使用了迭代): ``` void MergeSort(int arr[], int n) { int* temp = new int[n]; for (int len = 1; len < n; len *= 2) { for (int left = 0; left < n - len; left += len * 2) { int mid = left + len - 1; int right = min(left + len * 2 - 1, n - 1); int i = left, j = mid + 1, k = 0; while (i <= mid && j <= right) { if (arr[i] <= arr[j]) temp[k++] = arr[i++]; else temp[k++] = arr[j++]; } while (i <= mid) temp[k++] = arr[i++]; while (j <= right) temp[k++] = arr[j++]; for (int p = 0; p < k; p++) arr[left + p] = temp[p]; } } delete[] temp; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值