当前位置:网站首页>Insertion Sort
Insertion Sort
2022-08-05 22:04:00 【DeeGLMath】
插入排序(Insertion Sort)
一、基本思想
插入排序的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间.
二、实现逻辑
有一组数字:{5, 2, 4, 6, 1, 3},Arrange the group of numbers from smallest to largest.从第二个数字开始,Think of it as a newly added number,The second number just needs to be sorted by comparing it with the first number to the left;in the third number,Consider the first two sorted numbers to be the sorted cards in your hand,Then just compare the third number with the first two numbers;以此类推,Until the last number is compared with all previous numbers,插入排序完成.
算法步骤:
- 从第一个元素开始,该元素可以认为已经被排序;
- 取出下一个元素,在已经排序的序列中从后向前扫描;
- If an element in the sorted sequence is greater than the new element,则将该元素移到下一位置;
- 重复步骤 3,直到找到已排序的元素小于或等于新元素的位置;
- 将新元素插入到该位置;
- 重复步骤 2 ~ 5.
三、时间复杂度的分析
If the goal of insertion sort is to put n n n Sort a sequence of elements in ascending order,那么:
- If the sequence is already in ascending order,只需要比较 n − 1 n-1 n−1 次即可,That is, the best time complexity is : Ω ( n ) \Omega(n) Ω(n);
- If the sequence is in descending order,Then since the sequence has at most n ( n − 1 ) / 2 n(n-1)/2 n(n−1)/2 pair reverse order,即最坏时间复杂度为: O ( n 2 ) O(n^2) O(n2).
For the average time complexity,长度为 n n n 的序列有 n ! n! n! 种排列,Therefore, the permutation of each sequence is average n ( n − 1 ) / 4 n(n-1)/4 n(n−1)/4 pair reverse order,Therefore, the average time complexity is : Θ ( n 2 ) \Theta(n^2) Θ(n2).
四、空间复杂度的分析
Because the algorithm uses isin-place排序方法,No additional openings will be made O ( n ) O(n) O(n) 的数组空间(out-place排序),所以空间复杂度为: O ( 1 ) O(1) O(1).
五、算法实现
直接插入
def insertion_sort(array: List) -> None:
''' 支持数值型数据,如整型与浮点型混合;支持全为字符串类型的数据;不支持字符串型与数值型混合. '''
for index in range(1, len(array)):
key = array[index]
pre = index - 1
while pre >= 0 and key < array[pre]:
array[pre + 1] = array[pre]
pre -= 1
array[pre + 1] = key
折半插入
def insertion_sort(array: List) -> None:
''' 支持数值型数据,如整型与浮点型混合;支持全为字符串类型的数据;不支持字符串型与数值型混合. '''
for index in range(1, len(array)):
key = array[index]
low, high = 0, index - 1
while low <= high: # A sequence that conforms to monotonicity
mid = (low + high) // 2
if key > array[mid]:
low = mid + 1
else:
high = mid - 1
for pre in range(index, low, -1): # 从后往前
array[pre] = array[pre - 1]
array[low] = key
边栏推荐
猜你喜欢

nodejs(三)模块化,exports对象,npm与包的分类和结构,模块分类,模块作用域,exports对象,dependencies与devDepndencies节点,nrm,i5ting_toc

精益生产之MES制造执行系统

龙蜥社区第十次运营委员会议顺利召开!

对NotNull字段插入Null值有啥现象?

nodejs(一)fs模块(操作文件的模块),path路径模块,路径拼接path.join,抵消两层路径的写法,浏览器中的js

Qt使用wget下载文件案例

网页提示此站点不安全,还能打开吗?

MapReduce总结(未完待续)

CAN-Oe channel configuration method

地球系统模式(CESM)
随机推荐
【树莓派】树莓派安装OpenWrt
【frp】树莓派使用Frp内网穿透访问
CAN-Oe channel configuration method
我们公司是初次使用OKR,在落地时要特别注意哪些事情?
架构实战营课程学习感受
【树莓派】树莓派使用0.96吋OLED显示屏
印刷行业APS解决方案
网页提示此站点不安全,还能打开吗?
The pits and shortcomings encountered during the installation of kong and konga
树莓派红外控制空调
给初入测试/开发程序员的几点建议,把困难当做猎物......
double精确的加减乘除运算,并保留3位小数(3位后边的四舍五入和3位后边的直接舍去不要)
ESP8266-Arduino编程实例-TM1637-驱动4位7段数码管
杭电多校-Planar graph-(最大生成树+图树关系)
ESP8266-Arduino编程实例-金属触摸传感器驱动
食品加工行业MES与APS的应用
什么是 MySQL?SQL 数据库初学者教程和使用指南
Jelurida参会KBW2022,瑞士公链登陆韩国峰会
对NotNull字段插入Null值有啥现象?
《机器学习实战》第3章—隐形眼镜类型(Jupyter版决策树)