标签:continue 复杂 正数 个数 选择排序 开始 选择 insert strong
将数组元素插入已经有序的部分中,具体的过程是在有序的部分中通过比较找到新插入元素应该插入的位置,然后从有序部分的队尾元素开始,统统向后移动一位(这一位原本是刚刚那个元素的位置)直到应改插入的那个地方给腾出来,将元素放进去,重复上述过程,直到所有元素有序
def insertion_sort4(collection): length=len(collection) #外层循环负责从前到后,将所有元素向前插入 for loop_index in range(1,length): #内层循环负责在已经有序的序列中找到位置 current_element=collection[loop_index] #改进以更高效的处理有序(只能对升序或者降序的一种) if current_element>=collection[loop_index-1]: continue for i in range(loop_index): if collection[i]>current_element:#不选择等于,可以向后少移 #移动出位置 for j in range(loop_index,i,-1): #不选择从后往前交换,而是移动 collection[j]=collection[j-1] collection[i]=current_element break return collection
算法性能: 最优时间复杂度:O(n^2);最坏时间复杂度O(n^2);平均时间复杂度O(n^2)
原地排序,稳定和不稳定都能上面的是稳定的
随机数据(1000):运行100次,与快速排序比较,比快排慢一个数量级
详细数据:[0.02998447418, 0.03126454353, 0.03298068047, 0.03098273277, 0.0309882164, 0.03198075294, 0.03298044205, 0.03097939491, 0.03298044205, 0.03096532822, 0.03296637535, 0.03098273277, 0.031988 14392, 0.03198170662, 0.02998304367, 0.03198099136, 0.03198194504, 0.03098273277, 0.03129839897, 0.03198194504, 0.03198242188, 0.03304195404, 0.03298139572, 0.03397989273, 0.03098106384, 0.03098225594, 0.03298139572, 0.03297972679, 0.0310177803, 0.03232741356, 0.03198122978, 0.03098225594, 0.03198099136, 0.03098249435, 0.03098201752, 0.03397893906, 0.03098201752, 0.02998447418, 0.03198170662, 0.0309817791, 0.03298139572, 0.0319814682, 0.03198027611, 0.03198075294, 0.03298068047, 0.03215503693, 0.03098106384, 0.03198122978, 0.0319814682, 0.03098297119, 0.03198385239, 0.03198289871, 0.03198218346, 0.03234624863, 0.03198075294, 0.03098201752, 0.03298807144, 0.03098058701, 0.03098154068, 0.03098106384, 0.03098273277, 0.03198075294, 0.03198075294, 0.03098034859, 0.0321495533, 0.03198504448, 0.03298187256, 0.03198218346, 0.03198099136, 0.02998280525, 0.0319814682, 0.03098416328, 0.03098154068, 0.03397893906, 0.03198099136, 0.03331136703, 0.03198075294, 0.03198051453, 0.03098082542, 0.03198099136, 0.03298044205, 0.03497982025, 0.03198122978, 0.0319814682, 0.03497815132, 0.03497886658, 0.03298139572, 0.03198099136, 0.03099918365, 0.03297901154, 0.03299331665, 0.03199267387, 0.0319890976, 0.03297424316, 0.03127980232, 0.03294634819, 0.03094792366, 0.03395628929, 0.03296804428, 0.03299927711] 运行了100次,平均运行时间差(me-other)/(bubble-quick)(正数代表你是个弟弟)是:0.03200459957 前者(插入排序)平均运行时间0.03376406193,后者(快排)平均运行时间0.00175946236,前者约是后者的19.1900倍
与选择排序比较,比选择排序快一半
详细数据:[-0.04897117615, -0.05092978477, -0.04897165298, -0.04997181892, -0.04597568512, -0.05196714401, -0.04897165298, -0.04797005653, -0.0479722023, -0.0489730835, -0.0529692173, -0.05296993256 , -0.04297542572, -0.04998970032, -0.0499894619, -0.04997038841, -0.04599285126, -0.04995369911, -0.04894709587, -0.0499458313, -0.05196213722, -0.04893517494, -0.05195713043, -0.0509455204, -0.05097126961, -0.04797315598, -0.04997229576, -0.05100440979, -0.04995894432, -0.05094909668, -0.04797363281, -0.04899024963, -0.05000472069, -0.05099892616, -0.05095696449, -0.04995822906, -0.04898285866, -0.05094671249, -0.04994940758, -0.04894113541, -0.05093574524, -0.05198144913, -0.05096340179, -0.04999995232, -0.04898786545, -0.04995727539, -0.05194878578, -0.04994487762, -0.04897165298, -0.04999732971, -0.04838752747, -0.0509853363, -0.04996991158, -0.04995727539, -0.05197072029, -0.05199098587, -0.04898548126, -0.04898786545, -0.04996991158, -0.0499727726, -0.05294942856, -0.05097031593, -0.04997706413, -0.04994726181, -0.05197072029, -0.04798436165, -0.05097079277, -0.05095767975, -0.0499651432, -0.05096721649, -0.04897141457, -0.04895305634, -0.04997110367, -0.0509967804, -0.04997372627, -0.04997205734, -0.04995822906, -0.05198216438, -0.04995846748, -0.0509455204, -0.04898524284, -0.04997134209, -0.0499587059, -0.04998278618, -0.04897117615, -0.05296874046, -0.0509724617, -0.04797387123, -0.049949646, -0.04797267914, -0.04998350143, -0.05198264122, -0.04997396469, -0.04997444153, -0.05096530914, -0.05196070671, -0.04997181892, -0.04898834229, -0.04998731613, -0.04998636246] 运行了100次,平均运行时间差(me-other)/(bubble-quick)(正数代表你是个弟弟)是:-0.05001399517 前者(插入排序)平均运行时间0.03409122467,后者(冒泡)平均运行时间0.08410521984,前者约是后者的0.4053倍
标签:continue 复杂 正数 个数 选择排序 开始 选择 insert strong
原文地址:https://www.cnblogs.com/Gaoqiking/p/11169522.html