博客
关于我
7、归并排序法Merge()+MergePass()+MergeSort()
阅读量:71 次
发布时间:2019-02-26

本文共 2451 字,大约阅读时间需要 8 分钟。

归并排序是一种高效的稳定排序算法,由Tony Hoare提出,并且在1960年被Bob Floyd优化为更高效的形式。归并排序的基本思想是将数组分成两半,分别进行归并排序,然后将两半有序数组合并成一个有序数组。这种方法的时间复杂度是O(n log n),空间复杂度是O(n)。

归并排序的实现

归并排序的实现通常包括以下几个步骤:

  • 归并操作:将两个已经排好序的数组合并成一个有序数组。通常使用双向数组来提高效率。

  • 归并排序:递归地对数组进行分割,直到每个子数组只有一个元素为止,然后再逐步合并。

  • 优化:通过将常数倍增的子问题分解,减少递归深度,提高效率。

  • 代码实现

    以下是归并排序的伪代码实现:

    #include 
    #include
    #include
    #include
    #include
    using namespace std;const int MAXSIZE = 1001;// 归并排序函数void Merge(int r[], int r1[], int s, int m, int t) { int i = s, j = m + 1, k = s; while (i <= m && j <= t) { if (r[i] <= r[j]) r1[k++] = r[i++]; else r1[k++] = r[j++]; } if (i <= m) { while (i < m) r1[k++] = r[j++]; } else { while (j < t) r1[k++] = r[j++]; }}void mergepass(int r[], int r1[], int n, int h) { int i = 2 * i; while (i <= n - 2 * h + 1) { Merge(r, r1, i, i + h - 1, i + 2 * h - 1); if (i > n - h + 1) { Merge(r, r1, i, i + h - 1, n); } else { for (int k = i; k <= n; k++) r1[k] = r[k]; } i = 2 * i; }}void MergeSort(int r[], int r1[], int n) { int h = 1; while (h < n) { mergepass(r, r1, n, h); h *= 2; }}void mergesort() { // 递归实现}// 从外存读取数据到内存数组void read_data(int data[]) { ifstream infile("data_salary.txt", ios::in); if (!infile) { cerr << "open data_salary.txt error!" << endl; system("pause"); exit(1); } for (int i = 1; i < MAXSIZE; i++) { infile >> data[i]; } infile.close();}// 输出内存数组到外存文件void write_data(int data[]) { ofstream outfile("ordered_data_salary.txt", ios::out); if (!outfile) { cerr << "open ordered_data_salary.txt error!" << endl; } for (int i = 1; i < MAXSIZE; i++) { outfile << data[i] << endl; } outfile.close();}

    代码分析

  • 头文件包含iostreamfstreamcstdlibwindows.hctime,这些是常用的C++头文件。

  • 常量定义MAXSIZE定义了数组的最大规模。

  • 归并排序函数:包括MergemergepassMergeSortmergesort函数。

  • 读取数据:从文件data_salary.txt读取数据到数组data

  • 写数据:将排序后的数据写入文件ordered_data_salary.txt

  • 实现细节

  • 归并操作Merge函数负责将两个子数组合并成一个有序数组。

  • 归并排序mergepass函数负责对数组进行归并排序,MergeSort函数是非递归实现。

  • 递归优化:通过逐步增加归并长度h,减少递归深度,提高效率。

  • 应用示例

  • 读取数据:从外部文件读取数据并存储在数组中。

  • 排序数据:使用归并排序对数据进行排序。

  • 输出结果:将排序后的数据写入外部文件。

  • 优化建议

  • 减少递归深度:通过逐步增加归并长度h,减少递归深度,提高效率。

  • 使用双向数组:提高内存利用率,减少数据复制次数。

  • 优化常数倍增:通过调整常数倍增的方式,减少时间复杂度。

  • 处理异常:确保文件读写操作异常处理,避免程序崩溃。

  • 通过上述实现和优化,可以有效地对数据进行排序,并提高程序的运行效率。

    转载地址:http://hltz.baihongyu.com/

    你可能感兴趣的文章
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
    查看>>
    SQL-CLR 类型映射 (LINQ to SQL)
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>
    org/hibernate/validator/internal/engine
    查看>>
    Orleans框架------基于Actor模型生成分布式Id
    查看>>
    SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
    查看>>
    ORM sqlachemy学习
    查看>>
    Ormlite数据库
    查看>>
    orm总结
    查看>>
    os.environ 没有设置环境变量
    查看>>
    os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
    查看>>
    os.removexattr 的 Python 文档——‘*‘(星号)参数是什么意思?
    查看>>