更快的排序——归并排序!基于分而治之的对数复杂度的排序Merge Sort

Merge Sort Algorithm

The merge sort algorithm is defined recursively:

  • If the list is of size 1, it’s sorted, we are done.

  • Otherwise:

  • Divide an unsorted list into two sub-lists,
  • Sort each sub-list recursively using merge sort, and
  • Merge the two sorted sub-lists into a single sorted list

This strategy is called Divide and Conquer.

Programming a merge is straight-forward:

  • the sorted arrays, arr1 and arr2, are of size n1 and n2.

  • we have an empty array, arrOut, of size n1+n2.

And use three pointer to indicate where we are processing for each array.

int[] merge(int[] arr1, int[] arr2) {
    int p1 = 0, p2 = 0, k = 0;
    int n1 = arr1.length, n2 = arr2.length;
    int arrOut[] = new int[n1 + n2];
    while (p1 < n1 && p2 < n2) {
        // continue put smaller elements of arr1&arr2 to arrOut
        if (arr1[p1] < arr2[p2]) {
            arrOut[k] = arr1[p1++];
        } else {
            arrOut[k] = arr2[p2++];
        }
        ++k;
    }
    // put rest to arrOut
    while (p1 < n1) {
        arrOut[k++] = arr1[p1++];
    }
    while (p2 < n2) {
        arrOut[k++] = arr2[p2++];
    }
    return arrOut;
}

Time: we copied n1+n2 elements to arrOut array.

  • Hence, merging may performed in (\Theta(n1+n2)) time
  • If the two arrays are approximately the same size,(n=n1\approx n2), we can say that the run time is (\Theta(n)).

Space: we have to allocate a new array to merge the two array, can’t be done in-place. So the memory requirements are also (\Theta(n1+n2)).

In practice, if the list size is less than a threshold, use an algorithm like insertion sort, which is fast when array is small.

Suppose the function void merge(int[] arr, int a, int b, int c) will merge two sorted subarrays arr[a]~arr[b-1] and arr[b]~arr[c-1] into a single sorted array from index a to c-1.

void merge_sort(int[] arr, int first, int last):
Sort the entries from position first<=i< code> to <code>last>i</code>.<!--=i<-->

  • If the number of entries is less than N, call insertion sort.

  • Otherwise

  • Find the mid-point,
  • call merge sort recursively on each of the halves, and
  • merge the result
void mergeSort(int[] arr, int first, int last) {
    if (last - first

The time required to sort an array of size (n>1) is:

  • the time required to sort the first half
  • the time required to sort the second half, and
  • the time required to merge the two lists
    That is:

[T(n)=\begin{cases} \Theta(1)&n=1\ 2T(\frac{n}{2})+\Theta(n)&n>1 \end{cases} ]

So: (T(n)=\Theta(n\ln(n))).

The worst case and best case has the same run time, they are all (\Theta(n\ln(n))).

First, divide up problem into several subproblems(of the same kind).

Second, solve(conquer) each subproblem recursively.

Finally, combine solutions to subproblems into overall solution.

Resources: ShanghaiTech 2020Fall CS101 Algorithm and Data Structure.

Original: https://www.cnblogs.com/liuzhch1/p/16470853.html
Author: liuzhch1
Title: 更快的排序——归并排序!基于分而治之的对数复杂度的排序Merge Sort

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/607243/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • select,poll,epoll的区别以及使用方法

    I/O多路复用是指:通过一种机制,可以 监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作。 原生socket客户端在与服务端建立连接时,…

    Linux 2023年6月14日
    0108
  • python一键探测编码

    程序功能 按文件输出编码or按编码输出文件 源码 主要代码功能 1.实现文件遍历 2.chardet获取编码 3.传参,对符合编码条件的文件输出 4.打开文件夹选择对话框 程序功能…

    Linux 2023年6月7日
    0129
  • ACL和NAT

    NAT 概述: NAT(网络地址翻译)一个数据包目的ip或者源ip为私网地址, 运营商的设备 无法转发数据。 NAT工作机制: 一个数据包从企业内网去往公网时,路由器将数据包当 中…

    Linux 2023年6月6日
    0103
  • 关于多个 Cookie 的分隔符这件事

    对于 Cookie 的处理上,我最近遇到一个问题,那就是如何分割 Cookie 的内容。有人说是使用逗号分割,有人说是使用分号分割,究竟用哪个才是对的?其实这个答案是需要分为两个过…

    Linux 2023年6月6日
    092
  • aspx页面,后端通过Attributes.Add给textbox添加事件时,传参失效问题。

    测试一:————————————&#…

    Linux 2023年6月7日
    0102
  • shell编程-杨辉三角简单实现

    shell编程-杨辉三角问题: 概述:中国古代数学家在数学的许多重要领域中处于遥遥领先的地位。中国古代数学史曾经有自己光辉灿烂的篇章,而杨辉三角的发现就是十分精彩的一页。杨辉三角形…

    Linux 2023年6月7日
    0113
  • linux防火墙设置

    linux防火墙配置,开启/关闭防火墙服务,开放/禁止端口。 linux防火墙设置 环境:ubuntu 20.04 服务设置 开启防火墙 sudo ufw enable 查看防火墙…

    Linux 2023年6月13日
    0105
  • K8s-二进制安装

    K8S-二进制安装使用 1.IP总规划 服务类型 ip地址 组件 k8s-master01 etcd集群节点1 192.168.80.20 kube-apiserver、kube-…

    Linux 2023年6月13日
    0100
  • lambda跨账号调用elasticache redis调查结果

    1.本地lambda与被调用方的redis都要绑定一个VPC,至少设定一个子网和路由表,设定好安全组; 2.本地VPC创建对等连接,被调用方接受连接; 3.将各自的IPv4 CID…

    Linux 2023年5月28日
    079
  • ElasticSearch7.2安装

    下载JDK压缩包,通过SFTP客户端(WinSCP)上传到CentOS7相应的目录下。然后解压JDK,解压命令为: tar -zxvf jdk-12.0.2_linux-x64_b…

    Linux 2023年6月7日
    0113
  • 用shell抓取某考试试题

    一、背景 最近公司组织考信息安全,但考试机构没有整理出试题,只给了以下几个在线练习的链接,想着用博客整理下题库题型,奈何这个只能用拍照图片,然后用图片转文字的方式太慢,累死个人了,…

    Linux 2023年6月6日
    0106
  • JVM的监控

    JVM的监控 Table of Contents *一、jvm常见监控工具&指令 * 1、 jps:jvm进程状况工具 * 2、jstat: jvm统计信息监控工具 * 3…

    Linux 2023年6月13日
    0132
  • windows下使用route添加路由

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    Linux 2023年6月7日
    0111
  • 机器学习学习笔记之三:朴素贝叶斯

    条件概率和贝叶斯公式 (p(x|y)) 表示在 (y) 发生的条件下 (x) 发生的概率。 条件概率公式:已知 (p(x)) 和 (p(y)),以及(x), (y)同时发生的概率(…

    Linux 2023年6月14日
    069
  • Linux下无限期使用Navicat16

    原文链接:https://www.zhoubotong.site/post/79.htmllinux 下的数据库图形化工具比较好用的有dbeaver完全免费,相比navicat,我…

    Linux 2023年6月6日
    0149
  • Ubuntu下交换Alt和Ctrl (适用于任何按键修改)

    在 Ubuntu 下交换 Alt和 Ctrl键: sudo vim /usr/share/X11/xkb/keycodes/evdev 或使用系统默认编辑器打开: [En] Or …

    Linux 2023年5月27日
    0108
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球