博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 排序冒泡排序与双向冒泡排序
阅读量:5300 次
发布时间:2019-06-14

本文共 12147 字,大约阅读时间需要 40 分钟。

冒泡排序:

 冒泡排序就是每次找出最大(最小)元素,放在集合最前或最后,这是最简单的排序算法

def bubble_sort(collection):    #升序排列    length=len(collection)    for s in range(length-1):#可以假设只有一个元素的情况,这样可以直接返回        flage=True#应该放在这里,而不是上面        for i in range(length-1-s):            if collection[i]>collection[i+1]:#前者大需要换位置,并需要判断他是否是最大的                flage=False                collection[i],collection[i+1]=collection[i+1],collection[i]        # print("排序第",s+1,"轮之后:",collection)#print()好占时间啊        # i++#i是自动递增的,我竟然写出如此愚蠢的        if flage:            break    return collection

  特点:是稳定的 T(n)=O(n^2) 原地排序

  内层循环的操作是O(1)的,共执行n-1轮循环,每轮分别执行(n-1,n-2....1)=(n-1)(n-1+1)/2

双向冒泡排序:

  双向冒泡排序又称为:鸡尾酒排序

  双向冒泡作为冒泡排序的轻微改进,主要的不同就在于遍历元素时不只有从前到后,而是在遍历到序列的队尾时,按照从后向前在遍历到队头。

  以序列(2,3,4,5,1)为例,鸡尾酒排序只需要访问一次序列就可以完成排序,但如果使用则需要四次。但是在随机数序列的状态下,鸡尾酒排序与冒泡排序的效率都很差劲。

def bidirectional_bubble_sort3(collection):    length=len(collection)    disorder_start_index=0    disorder_end_index=length-1        while disorder_start_index
collection[disorder_start_index+1]: collection[disorder_start_index],collection[disorder_start_index+1]=collection[disorder_start_index+1],collection[disorder_start_index] for i in range(disorder_start_index+1,disorder_end_index): #一次找两个最大的元素,为什么是两个,因为不用第二次比较,假如比这两个中的第一个大就让第一个向后挪,如果比第一个小,就和第二个比较 if collection[i]>collection[i+1]: collection[i],collection[i+1]=collection[i+1],collection[i] flage=True if collection[i]
collection[j+1]: collection[j],collection[j+1]=collection[j+1],collection[j] disorder_start_index+=2 if not flage: break return collection

  算法分析:最坏时间复杂度O(n^2)  、平均时间复杂度O(n^2)、最优时间复杂度O(n)

但如果序列在一开始已经大部分排序过的话,会接近O(n)

对比

  与冒泡相比,比冒泡快20%,时间是冒泡的80%

详细数据:[-0.01099324226, -0.01499199867, -0.01399159431, -0.01399326324, -0.01499223709, -0.01399183273, -0.01599097252, -0.01499199867, -0.0160036087, -0.01500368118, -0.01399087906, -0.0149936676, -0.01599144936, -0.014991045, -0.01399230957, -0.01599121094, -0.01401019096, -0.01300311089, -0.01602697372, -0.01500558853, -0.01799058914, -0.01498985291, -0.01596426964, -0.01299071312, -0.01600933075, -0.01598501205, -0.01598405838, -0.01801800728, -0.0170276165, -0.01397919655, -0.01600432396, -0.13593554497, -0.01498842239, -0.01499223709, -0.01497864723, -0.01601624489, -0.01398897171, -0.01598596573, -0.01599144936, -0.01600551605, 0.05896472931, -0.01797199249, -0.01601552963, -0.01200699806, -0.01499462128, -0.0159740448, -0.01597189903, -0.01500415802, -0.01497507095, -0.01597762108, -0.0139913559, -0.01595687866, -0.00696969032, -0.01799035072, -0.01097488403, -0.01895284653, -0.01496243477, -0.01797008514, -0.0159766674, -0.02300024033, -0.0159907341, -0.02098846436, -0.01899337769, -0.008010149, -0.02000403404, -0.01698970795, -0.01698923111, -0.02098751068, -0.01400947571, -0.03299713135, -0.22087359428, -0.0069963932, -0.15491271019, -0.00699687004, -0.01550579071, -0.014991045, -0.01399278641, -0.0089943409, -0.01499080658, -0.04297423363, -0.01499080658, -0.01399040222, -0.01799154282, -0.01499199867, -0.01998782158, -0.01599144936, -0.01299262047, -0.01598978043, -0.01399302483, -0.01399111748, -0.01199412346, -0.01499032974, -0.0169904232, -0.01698660851, -0.01517891884, -0.0129904747, -0.01498866081, -0.01398968697, -0.01742386818, -0.01702928543]运行了100次,平均运行时间差(bidirectional_bubble_sort3-bubble_sort)(正数代表你是个弟弟)是:-0.01958100796前者(bidirectional_bubble_sort3)平均运行时间0.06751573801,后者(bubble_sort)平均运行时间0.08709674597,前者约是后者的0.7752倍

   与快排相比:是快排的40倍

详细数据:[0.0679602623, 0.06796073914, 0.06896018982, 0.06798624992, 0.06896018982, 0.06895971298, 0.06896018982, 0.068972826, 0.06895947456, 0.06896018982, 0.06698727608, 0.06896114349, 0.06997680664, 0.06896066666, 0.06794142723, 0.06797409058, 0.06996369362, 0.06894779205, 0.0699737072, 0.06796240807, 0.0689599514, 0.06696033478, 0.06995916367, 0.067943573, 0.06797242165, 0.06894803047, 0.06996655464, 0.06995368004, 0.06894516945, 0.06995940208, 0.06997895241, 0.06897211075, 0.0689599514, 0.06996059418, 0.06997251511, 0.06896066666, 0.06797456741, 0.06994271278, 0.0689458847, 0.06895947456, 0.06995916367, 0.07095837593, 0.06696271896, 0.07195830345, 0.06699585915, 0.07095932961, 0.06895923615, 0.06896018982, 0.06796050072, 0.06794905663, 0.06894779205, 0.06796050072, 0.06897377968, 0.07295536995, 0.0699596405, 0.06796121597, 0.06897592545, 0.06894993782, 0.0709373951, 0.0689599514, 0.06894612312, 0.07094478607, 0.06897330284, 0.06898331642, 0.06896018982, 0.06796145439, 0.06896018982, 0.0699596405, 0.07095837593, 0.06897234917, 0.06796836853, 0.06696081161, 0.06894207001, 0.06794261932, 0.06796050072, 0.0699596405, 0.06895637512, 0.06997203827, 0.06797218323, 0.0689599514, 0.06994652748, 0.06996011734, 0.06895565987, 0.06996059418, 0.0679602623, 0.06997108459, 0.07096338272, 0.06796073914, 0.06995820999, 0.06895947456, 0.06698918343, 0.06798553467, 0.0679731369, 0.06894683838, 0.06894683838, 0.06796240807, 0.06994342804, 0.06796813011, 0.06895875931, 0.0689599514]运行了100次,平均运行时间差(bidirectional_bubble_sort3-quick_sort2)(正数代表你是个弟弟)是:0.06901133537前者(bidirectional_bubble_sort3)平均运行时间0.07079039812,后者(quick_sort2)平均运行时间0.00177906275,前者约是后者的39.7908倍

  与归并相比:是归并的将近20倍

详细数据:[0.06596302986, 0.06396269798, 0.06496167183, 0.06296300888, 0.06696891785, 0.06696009636, 0.06396341324, 0.06496405602, 0.06996130943, 0.0649638176, 0.06496357918, 0.06296420097, 0.06596088409, 0.06196498871, 0.06296205521, 0.06596231461, 0.06596207619, 0.06496143341, 0.06396269798, 0.06396341324, 0.06496334076, 0.06496286392, 0.06296300888, 0.06396245956, 0.06396341324, 0.06396436691, 0.06796193123, 0.065959692, 0.06496143341, 0.06296300888, 0.06396269798, 0.06596088409, 0.06596183777, 0.06396245956, 0.06296300888, 0.06496238708, 0.06296253204, 0.0619635582, 0.06498837471, 0.06597709656, 0.06596016884, 0.06496167183, 0.06596231461, 0.06596708298, 0.06594824791, 0.06596112251, 0.06496214867, 0.06596660614, 0.06595754623, 0.06694293022, 0.06396174431, 0.06696128845, 0.06496310234, 0.06694960594, 0.0669362545, 0.06598424911, 0.06396317482, 0.06596112251, 0.06698656082, 0.06597089767, 0.06496024132, 0.06597447395, 0.06598377228, 0.06596159935, 0.06494235992, 0.06496167183, 0.06596207619, 0.06496191025, 0.0649638176, 0.06696105003, 0.06596183777, 0.06496334076, 0.06496286392, 0.06396198273, 0.0649626255, 0.06296253204, 0.06496238708, 0.06496167183, 0.06596207619, 0.0639629364, 0.06396317482, 0.06496405602, 0.06498575211, 0.0659661293, 0.06595015526, 0.06397628784, 0.06495499611, 0.06293916702, 0.06493544579, 0.06596136093, 0.06498217583, 0.06496167183, 0.06596159935, 0.07195830345, 0.06396317482, 0.06396317482, 0.06596207619, 0.06396174431, 0.06796145439, 0.06496310234]运行了100次,平均运行时间差(bidirectional_bubble_sort3-merge_sort3)(正数代表你是个弟弟)是:0.06517270088前者(bidirectional_bubble_sort3)平均运行时间0.06887008905,后者(merge_sort3)平均运行时间0.00369738817,前者约是后者的18.6267倍

  与希尔排序相比:是希尔排序的20倍

(sort) λ python some_sort.py详细数据:[0.06496214867, 0.06596159935, 0.06496286392, 0.06596207619, 0.06596207619, 0.06596112251, 0.06596136093, 0.06596660614, 0.06595993042, 0.06596326828, 0.06596112251, 0.06495976448, 0.065959692, 0.06696224213, 0.06498074532, 0.06692624092, 0.06395316124, 0.06694364548, 0.0659430027, 0.06797099113, 0.06594872475, 0.06396269798, 0.06598711014, 0.06498837471, 0.06596064568, 0.06395316124, 0.06495904922, 0.06698966026, 0.06594347954, 0.06594467163, 0.06598091125, 0.06594276428, 0.06594419479, 0.06496310234, 0.06895804405, 0.06596207619, 0.06496310234, 0.06496286392, 0.06696081161, 0.0669438839, 0.06593418121, 0.06594896317, 0.06699919701, 0.06696796417, 0.06695199013, 0.06596136093, 0.06498599052, 0.06496667862, 0.0659570694, 0.06391382217, 0.06594276428, 0.06597590446, 0.06693482399, 0.06596422195, 0.06694245338, 0.06696128845, 0.06497645378, 0.06500053406, 0.0659506321, 0.06793618202, 0.06594920158, 0.06596231461, 0.06797528267, 0.06598067284, 0.06594944, 0.06494998932, 0.06796479225, 0.06596183777, 0.06696128845, 0.06696081161, 0.0669798851, 0.06598258018, 0.06399250031, 0.06494641304, 0.0649895668, 0.06396317482, 0.06596374512, 0.06497192383, 0.06594514847, 0.06493616104, 0.06497645378, 0.0669503212, 0.06594324112, 0.06494402885, 0.06694865227, 0.06499385834, 0.06397032738, 0.06497907639, 0.06594586372, 0.0649497509, 0.06598567963, 0.06594824791, 0.0649626255, 0.06596302986, 0.06594586372, 0.06493163109, 0.06696295738, 0.06597995758, 0.06499958038, 0.06696271896]运行了100次,平均运行时间差(bidirectional_bubble_sort3-shell_sort3)(正数代表你是个弟弟)是:0.06586106062前者(bidirectional_bubble_sort3)平均运行时间0.06919025183,后者(shell_sort3)平均运行时间0.00332919121,前者约是后者的20.7829倍

  与插入排序相比:比插入慢,是其时间的2倍左右

详细数据:[0.03499317169, 0.03396701813, 0.03796577454, 0.03499388695, 0.03595423698, 0.03402686119, 0.03399848938, 0.03696060181, 0.03297901154, 0.03497552872, 0.03296375275, 0.03398108482, 0.03396248817, 0.03596591949, 0.03400611877, 0.03496527672, 0.03300404549, 0.03499364853, 0.03499746323, 0.03499794006, 0.03396677971, 0.03300309181, 0.0349817276, 0.03496956825, 0.03496623039, 0.04297542572, 0.03200554848, 0.03296232224, 0.03700256348, 0.03296637535, 0.03199267387, 0.03495168686, 0.03296113014, 0.0339653492, 0.03396773338, 0.04295682907, 0.03298091888, 0.03396201134, 0.03394603729, 0.03399896622, 0.03401470184, 0.03497982025, 0.0329682827, 0.03398132324, 0.03697824478, 0.03496456146, 0.032964468, 0.03499603271, 0.03398656845, 0.03500056267, 0.03497743607, 0.03496861458, 0.03198051453, 0.03301119804, 0.03498029709, 0.0329811573, 0.03496718407, 0.03498005867, 0.03297972679, 0.03398108482, 0.03398156166, 0.03398990631, 0.03296685219, 0.0310087204, 0.03398942947, 0.0319647789, 0.03597855568, 0.03496909142, 0.03296422958, 0.03497958183, 0.0349984169, 0.03496170044, 0.03397202492, 0.03300476074, 0.03299379349, 0.03299474716, 0.03499293327, 0.03394889832, 0.03499150276, 0.03497171402, 0.03794336319, 0.0339756012, 0.03396725655, 0.03196263313, 0.03396320343, 0.03501653671, 0.03497958183, 0.03299164772, 0.03298211098, 0.03599739075, 0.03400588036, 0.03201532364, 0.03499317169, 0.03597450256, 0.03496170044, 0.03801369667, 0.0339820385, 0.03498077393, 0.03398084641, 0.03498029709]运行了100次,平均运行时间差(bidirectional_bubble_sort3-insertion_sort4)(正数代表你是个弟弟)是:0.03445067883前者(bidirectional_bubble_sort3)平均运行时间0.06767181158,后者(insertion_sort4)平均运行时间0.03322113276,前者约是后者的2.0370倍

  与选择排序相比:比选择排序慢,是选择的2倍左右

详细数据:[0.02798295021, 0.03098106384, 0.02998280525, 0.02996516228, 0.03795814514, 0.03196191788, 0.03199958801, 0.03201460838, 0.0299642086, 0.03096485138, 0.03197264671, 0.03399443626, 0.03199982643, 0.03397583961, 0.03199720383, 0.03199839592, 0.03298139572, 0.03300595284, 0.0359787941, 0.02999973297, 0.03299283981, 0.03298425674, 0.03295087814, 0.03094530106, 0.02994942665, 0.0329785347, 0.03194737434, 0.03298020363, 0.03097820282, 0.02999544144, 0.03301692009, 0.03300237656, 0.03396272659, 0.03398346901, 0.03198122978, 0.03298258781, 0.03200244904, 0.0319814682, 0.03098106384, 0.0300142765, 0.03398036957, 0.03401565552, 0.03099370003, 0.03095555305, 0.03199458122, 0.03099370003, 0.03498530388, 0.03397893906, 0.0319750309, 0.03297185898, 0.03401732445, 0.03198838234, 0.02898311615, 0.08495044708, 0.03097605705, 0.03498649597, 0.03095364571, 0.03195500374, 0.02899241447, 0.03494858742, 0.03295993805, 0.03196334839, 0.03396272659, 0.03295135498, 0.03197288513, 0.03097701073, 0.03300070763, 0.03199863434, 0.03398823738, 0.03297519684, 0.03296017647, 0.03296780586, 0.03399276733, 0.03398513794, 0.03099942207, 0.03196954727, 0.03098726273, 0.03196763992, 0.03496670723, 0.03401255608, 0.03197717667, 0.03097891808, 0.0310177803, 0.03499245644, 0.03198575974, 0.03298282623, 0.03697443008, 0.03396677971, 0.03198218346, 0.0319955349, 0.03196620941, 0.03297662735, 0.03196763992, 0.03096342087, 0.03297567368, 0.03198027611, 0.03296375275, 0.03198623657, 0.0329709053, 0.03398156166]运行了100次,平均运行时间差(bidirectional_bubble_sort3-select_sort2)(正数代表你是个弟弟)是:0.03293031931前者(bidirectional_bubble_sort3)平均运行时间0.06903054476,后者(select_sort2)平均运行时间0.03610022545,前者约是后者的1.9122倍

  

 

  

转载于:https://www.cnblogs.com/Gaoqiking/p/11185715.html

你可能感兴趣的文章
一步步教你轻松学奇异值分解SVD降维算法
查看>>
使用pager进行分页
查看>>
UVA - 1592 Database
查看>>
Fine Uploader文件上传组件
查看>>
javascript中的传递参数
查看>>
objective-c overview(二)
查看>>
python查询mangodb
查看>>
consonant combination
查看>>
驱动的本质
查看>>
Swift的高级分享 - Swift中的逻辑控制器
查看>>
Swagger简单介绍
查看>>
Python数据分析入门案例
查看>>
vue-devtools 获取到 vuex store 和 Vue 实例的?
查看>>
Linux 中【./】和【/】和【.】之间有什么区别?
查看>>
内存地址对齐
查看>>
看门狗 (监控芯片)
查看>>
#ifndef #define #endif
查看>>
css背景样式
查看>>
JavaScript介绍
查看>>
开源网络漏洞扫描软件
查看>>