码迷,mamicode.com
首页 > 编程语言 > 详细

算法导论2.3.1合并排序

时间:2015-02-03 21:14:23      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

// 2.3.1 merge.h
#include <stdint.h>
// MERGE(A, p, q, r)
// n1 = q - p + 1
// n2 = r - q
// let L[1..n1 + 1] and R[1..n2 + 1] be new arrays
// for i = 1 to n1
//     L[i] = A[p + i - 1]
// for j = 1 to n2
//     R[j] = A[q + j]
// L[n1 + 1] = INT64_MAX
// R[n2 + 1] = INT64_MAX
// i = 1
// j = 1
// for k = p to r
//     if L[i] <= R[j]
//         A[k] = L[i]
//         i = i + 1
//     else A[k] = R[j]
//         j = j + 1

void merge(int64_t* A, uint64_t p, uint64_t q, uint64_t r)
{
    uint64_t n1 = q - p + 1;
    uint64_t n2 = r - q;
    int64_t* L = new int64_t[n1 * sizeof(int64_t)];
    int64_t* R = new int64_t[n2 * sizeof(int64_t)];
    for (uint64_t i = 0; i < n1; i++)
    {
        L[i] = A[p + i];
    }
    for (uint64_t j = 0; j < n2; j++)
    {
        R[j] = A[q + j + 1];
    }
    L[n1] = INT64_MAX;
    R[n2] = INT64_MAX;
    uint64_t i = 0;
    uint64_t j = 0;
    for (uint64_t k = p; k < r + 1; k++)
    {
        if (L[i] <= R[j])
        {
            A[k] = L[i];
            i++;
        }
        else
        { 
            A[k] = R[j];
            j++;
        }
    }
}

// MERGE-SORT(A, p, r)
// if p < r
//     q = |_(p + r) / 2_|
//     MERGE-SORT(A, p, q)
//     MERGE-SORT(A, q + 1, r)
//     MERGE(A, p, q, r)

void merge_sort(int64_t* A, uint64_t p, uint64_t r)
{
    if (p < r)
    {
        uint64_t q = (p + r) / 2;
        merge_sort(A, p, q);
        merge_sort(A, q + 1, r);
        merge(A, p, q, r);
    }
}

// 2.3.1 merge.cpp
#include <iostream>

#ifdef __linux
#include <stdio.h>
#endif

#include "merge.h"

using std::cout;
using std::cin;
using std::endl;

int main()
{
#if __WORDSIZE == 64
    std::cout << "WORD_SIZE == 64" << std::endl;
#endif
    int const n(10);
    int64_t a[n] = { 9, 1, 3, 10, 7, -3, 4, 2, 5, 0 };

    merge_sort(a, 0, n - 1);
    cout << "The sorted array is" << endl;
    for(int i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << endl;

    getchar();
    return 0;
}

 

合并排序模版

// merge_t.h
#include <stdint.h>
// MERGE(A, p, q, r)
// n1 = q - p + 1
// n2 = r - q
// let L[1..n1 + 1] and R[1..n2 + 1] be new arrays
// for i = 1 to n1
//     L[i] = A[p + i - 1]
// for j = 1 to n2
//     R[j] = A[q + j]
// L[n1 + 1] = INT64_MAX
// R[n2 + 1] = INT64_MAX
// i = 1
// j = 1
// for k = p to r
//     if L[i] <= R[j]
//         A[k] = L[i]
//         i = i + 1
//     else A[k] = R[j]
//         j = j + 1

template <class TYPE>
void merge_t(TYPE* A, uint64_t p, uint64_t q, uint64_t r)
{
    uint64_t n1 = q - p + 1;
    uint64_t n2 = r - q;
    TYPE* L = new TYPE[n1 * sizeof(TYPE)];
    TYPE* R = new TYPE[n2 * sizeof(TYPE)];
    for (uint64_t i = 0; i < n1; i++)
    {
        L[i] = A[p + i];
    }
    for (uint64_t j = 0; j < n2; j++)
    {
        R[j] = A[q + j + 1];
    }
    L[n1] = INT64_MAX;
    R[n2] = INT64_MAX;
    uint64_t i = 0;
    uint64_t j = 0;
    for (uint64_t k = p; k < r + 1; k++)
    {
        if (L[i] <= R[j])
        {
            A[k] = L[i];
            i++;
        }
        else
        { 
            A[k] = R[j];
            j++;
        }
    }
}

// MERGE-SORT(A, p, r)
// if p < r
//     q = |_(p + r) / 2_|
//     MERGE-SORT(A, p, q)
//     MERGE-SORT(A, q + 1, r)
//     MERGE(A, p, q, r)

template <class TYPE>
void merge_sort_t(TYPE* A, uint64_t p, uint64_t r)
{
    if (p < r)
    {
        uint64_t q = (p + r) / 2;
        merge_sort_t(A, p, q);
        merge_sort_t(A, q + 1, r);
        merge_t(A, p, q, r);
    }
}

// merge_t.cpp
#include <iostream>

#include "merge_t.h"

using std::cout;
using std::cin;
using std::endl;

int main()
{
    int const n(10);
    int64_t a[n] = { 9, 1, 3, 10, 7, -3, 4, 2, 5, 0 };

    merge_sort_t(a, 0, n - 1);
    cout << "The sorted array is" << endl;
    for(int i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << endl;

    getchar();
    return 0;
}

 

技术分享

算法导论2.3.1合并排序

标签:

原文地址:http://www.cnblogs.com/sunyongjie1984/p/4271022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!