更快的排序——归并排序!基于分而治之的对数复杂度的排序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)

大家都在看

  • Linux服务器下oracle数据库启动服务操作步骤

    一、在Linux下启动Oracle1.登录到Linux服务器,切换到oracle用户权限(命令是:# su –l oracle) 2.进入sqlplus界面(命令是:$ sqlpl…

    Linux 2023年6月13日
    093
  • 高通方案的Android设备几种开机模式的进入与退出

    高通方案的Android设备主要有以下几种开机模式,Android、EDL、Fastboot、Recovery和FFBM,其进入及退出的方式如下表。 adb reboot boot…

    Linux 2023年6月7日
    0130
  • 文本操作find cut sort wc sed awk

    文本操作 查找文件: # find 大概位置 以名字查找 名字 find /etc/ -name i18n find /etc/ -name 70* find /etc/ -nam…

    Linux 2023年6月11日
    092
  • 001.云桌面整体解决方案实施

    桌面云概述 桌面云介绍 本桌面云整体交付方案基于深信服aDesk桌面云实现。 深信服桌面云是采用云计算的思想,将用户的桌面操作系统以服务的形式通过网络进行交付,可以让用户在不同设备…

    Linux 2023年6月13日
    0109
  • linux自动备份mysql数据库

    备份脚本记录一下–(单个数据库) 2021-11-15 1.新建shell脚本:vim **.sh #!/bin/bashCKUP=/data/backup/db #获…

    Linux 2023年5月27日
    0108
  • oracle 怎么查看用户对应的表空间

    oracle 怎么查看用户对应的表空间? 查询用户: 查看数据库里面所有用户,前提是你是有 dba 权限的帐号,如 sys,system: select * from dba_us…

    Linux 2023年6月6日
    0104
  • Shell文件属性的判断与比较

    Shell支持对文件属性的判断,常用的文件属性操作符很多,如下表所示。更多文件属性操作符可以参考命令帮助手册man test [root@centos7&#xFF5E;]#…

    Linux 2023年6月6日
    080
  • 《卡死你3000》批量文件复制命令详解

    卡死你3000简介: 名词解释: 批量顺序复制文件:从主控机,到从被控机1,被控机2,复制文件。有卡住问题。 批量并发复制文件:从主控机,到从被控机1,被控机2,复制文件。使用多线…

    Linux 2023年6月13日
    0115
  • Ubuntu20.04 命令行 修改系统IP地址

    Ubuntu 修改IP地址(静态IP) 配置文件修改 — 命令行修改 ifconfig的安装及使用,ip 命令的使用 0. 前言 1. 修改配置文件 1.1 输入(修改…

    Linux 2023年6月6日
    0182
  • APACHE快速安装流程梳理

    快速安装开始: 【环境配置1】 yum -y install gcc gcc-c++ wget 保留操作(可跳过): yum -y removeapr-util-devel apr…

    Linux 2023年6月6日
    086
  • 剑指offer计划21( 位运算简单)—java

    1.1、题目1 剑指 Offer 15. 二进制中1的个数 1.2、解法 通过判断每一位的与来识别1的数量。 1.3、代码 public class Solution { // y…

    Linux 2023年6月11日
    0121
  • 【论文笔记】(FGSM)Explaining and Harnessing Adversarial Examples

    本文发表于 ICLR 2015,提出了经典的攻击方法 – FGSM(Fast Gradient Sign Method),这篇博客的第1-5节为重点部分,包括原文第5节…

    Linux 2023年6月7日
    0101
  • redis

    redis 慢 开启 AOF 1、多加服务器 2、增加写的能力 +ssdb Original: https://www.cnblogs.com/y896926473/p/96929…

    Linux 2023年5月28日
    077
  • Redis16个常见使用场景

    目录 缓存 数据共享分布式 分布式锁 全局ID 计数器 限流 位统计 购物车 用户消息时间线timeline 消息队列 抽奖 点赞、签到、打卡 商品标签 商品筛选 用户关注、推荐模…

    Linux 2023年5月28日
    0108
  • 阿里云OSS + PicGo搭建图床

    配置 PicGo 下载安装完成后,打开 PicGo,配置阿里云 OSS。 其中,KeyId 即创建 RAM 用户的 AccessKey ID,KeySecret 即 AccessK…

    Linux 2023年6月7日
    0119
  • JDK8以上提高开发效率

    1 接口的默认方法和静态方法 1.1 接口中可以定义默认方法和静态方法。 默认方法使用default修饰,静态方法和默认方法可以多个; 静态方法通过接口直接调用,默认方法通过接口实…

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