merge sort

  1. After Quicksort, this is the second efficient sorting algorithm
  2. Merge Sort operates on the “divide and conquer” principle:
  3. first
  4. second
  5. The array is divided until arrays of length 1 are created
  6. the number of division stages is log2 n.
  7. On each merge stage, we have to merge a total of n elements (on the first stage n × 1, on the second stage n/2 × 2, on the third stage n/4 × 4, etc.):
  8. The merge process does not contain any nested loops, so it is executed with linear complexity: If the array size is doubled, the merge time doubles, too. The total effort is, therefore, the same at all merge levels.
  9. The time complexity of Merge Sort is: O(n log n)
  10. Merge Sort is therefore no faster for sorted input elements than for randomly arranged ones.
  11. For presorted elements, Merge Sort is about three times faster than for unsorted elements.
  12. For elements sorted in descending order, Merge Sort needs a little more time than for elements sorted in ascending order.
  13. Merge Sort is about three times faster for pre-sorted elements than for unsorted elements. However, the number of comparison operations differs by only about one third.
  14. not in-place
  15. a stable sorting process.
  16. In-Place Merge Sort


    If the element above the left merge pointer is less than or equal to the element above the right merge pointer, the left merge pointer is moved one field to the right.
    Otherwise, all elements from the first pointer to, but excluding, the second pointer are moved one field to the right, and the right element is placed in the field that has become free. Then both pointers are shifted one field to the right, as well as the end position of the left subarray.
  17. Merge Sort is an efficient, stable sorting algorithm with an average, best-case, and worst-case time complexity of O(n log n).

insert sort
18. With the outer loop, this is obvious as it counts up to n.
19. Average Time Complexity

  1. the time complexity for shifting is, therefore, O(n²). This is also called “quadratic time”.
  2. For comparison operations, we have one more than shift operations (or the same amount if you move an element to the far left). The time complexity for the comparison operations is, therefore, also O(n²).
  3. Worst-Case Time Complexity

    The term from the average case, therefore, changes in that the second dividing by two is omitted:
    6 × 5 × ½
    The worst-case time complexity of Insertion Sort is: O(n²)
  4. Best-Case Time Complexity
    If the elements already appear in sorted order, there is precisely one comparison in the inner loop and no swap operation at all.
  5. Insertion Sort With Binary Search?
    we would not have gained anything from this, because we would still have to shift each element from the insertion position one position to the right, which is only possible step by step in an array. Thus the inner loop would remain at linear complexity despite the binary search. And the whole algorithm would remain at quadratic complexity, that is O(n²).
  6. Insertion Sort With a Linked List?

If the elements are in a linked list, couldn’t we insert an element in constant time, O(1)?

Yeah, we could. However, a linked list does not allow for a binary search. This means that we would still have to iterate through all sorted elements in the inner loop to find the insertion position. This, in turn, would result in linear complexity for the inner loop and quadratic complexity for the entire algorithm.

stable
Insertion Sort is not directly parallelizable.* However, there is a parallel variant of Insertion Sort: Shellsort

  1. Insertion Sort is, therefore, not only faster than Selection Sort in the best case but also the average and worst case.

The reason for this is that Insertion Sort requires, on average, half as many comparisons. As a reminder, with Insertion Sort, we have comparisons and shifts averaging up to half of the sorted elements; with Selection Sort, we have to search for the smallest element in all unsorted elements in each step.

Selection Sort has significantly fewer write operations, so Selection Sort can be faster when writing operations are expensive. This is not the case with sequential writes to arrays, as these are mostly done in the CPU cache.

DS-fundation-sort1相关推荐

  1. 2021年大数据Spark(二十五):SparkSQL的RDD、DF、DS相关操作

      目录 RDD.DF.DS相关操作 SparkSQL初体验 SparkSession 应用入口 获取DataFrame/DataSet 使用样例类 指定类型+列名 自定义Schema ​​​​​​​ ...

  2. DS, ES, SS, DI, SI, BP, SP, IP, FS 寄存器

    为什么80%的码农都做不了架构师?>>>    DS, ES, SS, DI, SI, BP, SP, IP, FS 寄存器 DS is called data segment re ...

  3. DSAPI多功能组件编程应用-DS提示气泡

    首先下载DSAPI.dll.并在项目中引用. 该功能包括在DSAPI1.0.1.1及更高版本号,DLL请到本人资源里查找. Private Sub Button1_Click(sender As Ob ...

  4. Windows Server 2012 之配置AD DS

    在企业级的局域网络中,通常需要对其网络中的所有计算机统一管理,而传统的方式(用户组处于工作组模式)计算机之间互相不信任(不然会导致很明显的安全问题 总不至于要去信任一个***的电脑吧?) 为此需要一个 ...

  5. 确认AD DS域是否正常

    一.检查DNS服务器内的日志是否完整 1.检查主机日志: 域控dc1.sayms.com已经正确的将其主机名与ip地址注册到DNS服务器内 2.检查SRV日志--使用DNS控制台 数据类型为" ...

  6. Azure上A/D系列虚拟机到DS系列迁移(2)

    11. 该脚本会检测环境,停止虚拟机,拷贝VHD文件,创建磁盘等等,知道出现如下类似画面,他会暂停等待用户操作: 后面这几个步骤非常重要,关系到你后续的创建是否成功: 12. 进入D:\migvm目录 ...

  7. plspl和oracle,LOL2017LSPL春季死啊DS轻取NON ME逆转GD赢得首胜

    LOL2017LSPL春季死啊DS轻取NON ME逆转GD赢得首胜.2月9日,2017<英雄联盟>甲级职业联赛(LSPL)春季常规赛第二周继续进行,在当天的比赛当中,DS在两局比赛中击败N ...

  8. N-MOS的G-S电容随着DS电压的变化关系

    ➤ 01 栅极-源极输入电容与DS电压 对于 MOS管 往往存在较大的输入电容.这个电容随着DS的变化关系使得它可以当做一个可变电容来使用. 下面是IRF3710的数据手册的输入电容与DS电压之间的关 ...

  9. 对Exchange 事件ID 9154 DSACCESS 返回 DS 通知出现的错误“0x80004005”的处理

    环境: 1台主DC(2k3,集成DNS)5种操作主机角色都运行在主DC上,一台额外域控.           1台EX2K3 问题1:现EX2k3服务器上出现如下错误日志:             事 ...

  10. Windows 08 R2_创建AD DS域服务(图文详解)

    目录 目录 Active Directory概念 创建第一个AD域控制器 搭建DNS服务器 使用Windows窗口程序创建AD域控制器 AD与LDAP的关系 使用Powershell来创建ADDS域控 ...

最新文章

  1. python是什么编程教程-编程python是什么_谁的Python教程最好?
  2. python 飞机大战小游戏
  3. Redis基础系列-0x003:String
  4. 如何拆计算机主机箱,一种方便拆卸的计算机主机箱的制作方法
  5. 【JAVA高级】——myEclipse连接mysql启动数据库服务
  6. Java中的package、import、import static
  7. 使用Volley+OkHttp+Gson加速Android网络开发
  8. 2022安全员-C证考试题库及答案
  9. 教你使用jmeter实现接口性能测试
  10. 回归分析的五个基本假设
  11. Android启动优化
  12. Linux中fork函数详解
  13. 依据MPU6050角速度原始数据的计步算法
  14. 用什么语言写游戏代码最好?
  15. Matlab多项式和符号函数简介
  16. 如何解决Unsupported Architecture. Your executable contains unsupported architecture '[x86_64, i386]
  17. 神州数码交换机路由器防火墙ACAP基本配置
  18. c++《AVL树的概念》《AVL树的插入》《AVL树的旋转》《AVL树的验证》《AVL树的删除》《AVL树的性能》
  19. 秋季出生的宝宝这样起名,全班同学都羡慕。
  20. ICMP协议是什么协议?

热门文章

  1. Python工程师基础班
  2. Springboot整合Neo4j和Mysql多数据源整合方案及其解决冲突问题方法
  3. 【灰色神经预测】基于布谷鸟算法优化灰色神经网络模型实现数据回归预测含Matlab源码
  4. 应用角度理解H264码流
  5. 脉冲神经网络 计算机科学,基于卷积计算的多层脉冲神经网络的监督学习 - 计算机工程与科学.PDF...
  6. 虚拟技术所能应用的几个方面及... ...
  7. 大一下学期.学期总结
  8. 时钟失效之后,STM32还能运行?
  9. AlphaGo Zero的启示:监督学习和无监督学习的利弊
  10. 学习笔记(8):让机器“看见”—计算机视觉原理及实战-(1.1)座标系与座标变化