文章首发于CSDN,博主HanSmileLiao链接原文链接:https:blog。csdn。netNebulaChienarticledetails120436663 转眼大二了,突然感觉比大一还要迷茫(也可能是因为数模竞赛,评优都没有搞好,明年暑假的智能车也一点没有头绪23333),计二报的是Python(虽然没什么卵用,但是学校要我们搞非精准扶贫),在用多了自带的函数之后,突然想起去年让我相爱相杀的排序算法,emmm专业课刚好遇到了数据结构里面的各种排序,就想着过了一年再系统地整理一下,也是因为自己对于C太生疏了吧,虽然我不知道C学了个什么玩意儿,真正让自己有点感触的还是OOP,毕竟万物皆对象,行为皆方法。打算做一个系列,再回头看看自己曾经学过的东西,数电模电,线性代数高数等等。 打算以后多在CSDN、Github、头条丢一些大佬压根看不上的垃圾,说不定哪天大佬就出来教我做事了呢:( 本人超级菜,求求大佬们出来教我做事啊 打算做一个系列,专门整理一些算法,先从排序开始一、两种排序算法的基本思想 1、冒泡法(起泡排序): 用到for循环,round1的两次for循环分别确定List〔0〕和List〔1〕,现在我们就得到了两个相邻的元素,按照期望的序列决定是较小的数上浮还是较大的数上浮,一直到List〔lengthList〕(以升序为例),此时相当于找到了序列0lengthList的最大值;round2的两次for循环分别确定List〔1〕和List〔2〕,以此类推一直到List〔lengthList1〕,此时找到了序列0lengthList1的最大值依次往后直到确定List〔0〕与List〔1〕大小关系,此时程序结束,如下所示:for(intjlen1;ji;j){if(nameList〔j〕nameList〔j1〕){n;Exchange(nameList〔j〕,nameList〔j1〕);Flagtrue;Aftertheexchangeisperformed,theboolvaluechanges}} before: 589637。。。。。(58)9637。。。。。5(89)637。。。。。58(69)37。。。。。586(39)7。。。。。5863(79)Round1finish。。。其实就相当于把数组拆分,长度依次减去一,每次操作的结果就是将对应数组的最小值(最大值)放在一端,当程序结束,相当于完成了排序。 2、选择法排序: 其实原理和冒泡排序类似,只不过不再是相邻两个比较,而是直接在拆分好的子序列里面找到最值,并放到最前面,这就好理解为什么同样一个序列,选择法排序执行次数要比冒泡排序少很多:Indexmaxi;for(intji1;jlen;j){n;if(nameList〔Indexmax〕nameList〔j〕){Indexmaxj;}}if(Indexmax!i){Exchange(nameList〔Indexmax〕,nameList〔i〕);}Supposethesequenceis:5286473Infirstroundthemaximumis8,i0,putitonlocation0,andthenewsequenceis:8526473Insecondroundthemaximumis7,i1,putitonlocation1,andthenewsequenceis8752643。。。。二、具体步骤 1、引入库 由于生成数列用的是随机数方式,而且为了保证每次运行程序所得到的结果都不一样,用到了以下库:includeiostreamincludestdlib。hincludetime。h 2。生成随机数 参考大佬的文章后用了动态生成随机数以及动态分配数组长度的方法:intListnewint〔lengthList〕;srand((unsignedint)time(NULL));for(intindex0;indexlengthList;index){List〔index〕rand()1001;} 结果每次运行程序都会得到不同的0100之间的随机数组三、具体代码 起泡排序:Createarandomlist,sortitwithupwardsanddownwardssequenceincludeiostreamincludestdlib。hincludetime。husingnamespacestd;Bubblesort:Basicidea:Compareadjacentelementsinturn,smallergoup(upwardssort)andbiggergodown;orbiggergoup(downwardssort)Eachturnwecomparepositionandpositionnext,ifpositionanditsnextmeettherequests,wedontneedtodoitagainSothealgorithmcanbeoptimized:BecauseeachBubbleactuallyisswitchthevalueWecouldsetaBOOLvalueasakey,eachcompareinitializethekeytofalse,andaftercomparingturnittotrueAftercomparingcheckkeyvalue,ifitsvalueisfalseinterrupttheprogramBubbleSort1isupwardssortBubbleSort2isdownwardssortvoidExchange(inta,intb){inttemp;tempa;ab;btemp;}Swap()isaplacethatiseasytoignore。Exchangevaluesareexchangedaddresses,orexchangetheirrespectivereferences(alias)voidBubbleSort1(intnameList〔〕,intlengthList){intm0,n0;intlenlengthList;boolFlag;Threshold(key),ifacertaintimehasbeensorted,thereisnoneedtocontinuetotraversedownfor(inti0;ilen;i){m;Flagfalse;for(intjlen1;ji;j){if(nameList〔j〕nameList〔j1〕){n;Exchange(nameList〔j〕,nameList〔j1〕);Flagtrue;Aftertheexchangeisperformed,theboolvaluechanges}}if(!Flag)break;Iftheboolvaluehasnotchanged,thereisnoexchange,jumpingoutofthiscycle,thatistosay,thistimetheordermeetstherequirementsif(Flag!true)break;}coutSortthelistwiththeformatofupis:endl;for(inti0;ilen;i){coutnameList〔i〕;}coutBooltime:mnendl;Justlikethis:before:589637。。。。。(58)9637。。。。。5(89)637。。。。。58(69)37。。。。。586(39)7。。。。。5863(79)Round1finish。。。}voidBubbleSort2(intnameList〔〕,intlengthList){intm0,n0;intlenlengthList;boolFlag;for(inti0;ilen;i){m;Flagfalse;for(intjlen1;ji;j){if(nameList〔j〕nameList〔j1〕){n;Exchange(nameList〔j〕,nameList〔j1〕);Flagtrue;}}if(!Flag)break;}coutSortthelistwiththeformatofdownis:endl;for(inti0;ilen;i){coutnameList〔i〕;}coutBooltime:mnendl;}intmain(void){intlengthList;intFlag1;while(Flag){coutEnterthelengthofrandomlist(length10):;cinlengthList;if(lengthList10){Flag0;coutExitbyfaultinputendl;break;}else{intListnewint〔lengthList〕;srand((unsignedint)time(NULL));for(intindex0;indexlengthList;index){List〔index〕rand()1001;}coutOriginalsort:endl;for(intindex0;indexlengthList;index){coutList〔index〕;}coutUpwardssort:endl;BubbleSort1(List,lengthList);coutDownwardssort:endl;BubbleSort2(List,lengthList);coutType1tocontinueand0toexit:;cinFlag;}}return0;} 选择法排序:UpwardsFirstround,findminimumvalueofList〔lengthlist〕,putitonfirstlocationindexSecondround,findminimumvalueofList〔lengthlist〕,putitonfirstlocationindex1。。。DownwardsFirstround,findmaximumvalueofList〔lengthlist〕,putitonfirstlocationindexSecondround,findmaximumvalueofList〔lengthlist〕,putitonfirstlocationindex1。。。includeiostreamincludestdlib。hincludetime。husingnamespacestd;voidExchange(inta,intb){inttemp;tempa;ab;btemp;}SelectionSort1()isupwardsSelectionSort2()isdownwardsvoidSelectionSort1(intnameList〔〕,intlengthList){intlenlengthList;intIndexmin;intm0,n0;for(inti0;ilen;i){m;Indexmini;for(intji1;jlen;j){n;if(nameList〔Indexmin〕nameList〔j〕){Indexminj;}}if(Indexmin!i){Exchange(nameList〔Indexmin〕,nameList〔i〕);}Supposethesequenceis:5286473Infirstroundtheminimumis2,i0,putitonlocation0,andthenewsequenceis:2586473Insecondroundtheminimumis3,i1,putitonlocation1,andthenewsequenceis2358647。。。。}coutTheupwardssortis:endl;for(intindex0;indexlengthList;index){coutnameList〔index〕;}coutendl;coutNumberofexecutions:mnendl;}voidSelectionSort2(intnameList〔〕,intlengthList){intlenlengthList;intIndexmax;intm0,n0;for(inti0;ilen;i){m;Indexmaxi;for(intji1;jlen;j){n;if(nameList〔Indexmax〕nameList〔j〕){Indexmaxj;}}if(Indexmax!i){Exchange(nameList〔Indexmax〕,nameList〔i〕);}Supposethesequenceis:5286473Infirstroundthemaximumis8,i0,putitonlocation0,andthenewsequenceis:8526473Insecondroundthemaximumis7,i1,putitonlocation1,andthenewsequenceis8752643。。。。}coutThedownwardssortis:endl;for(intindex0;indexlengthList;index){coutnameList〔index〕;}coutendl;coutNumberofexecutions:mnendl;}intmain(void){intlengthList;intFlag1;while(Flag){coutEnterthelengthofrandomlist(length10):;cinlengthList;if(lengthList10){Flag0;coutExitbyfaultinputendl;break;}else{intListnewint〔lengthList〕;srand((unsignedint)time(NULL));for(intindex0;indexlengthList;index){List〔index〕rand()1001;}coutOriginalsort:endl;for(intindex0;indexlengthList;index){coutList〔index〕;}coutendl;SelectionSort1(List,lengthList);coutendl;SelectionSort2(List,lengthList);coutType1tocontinueand0toexit:;cinFlag;}}return0;} 以上就是第一篇关于最基础的两种排序算法的分享,大家还有什么想了解的可以打在评论区,我会逐一解答,以后也会多多分享这些资源。 要走的路还很长啊,共勉! 祝大家周末快乐!〔比心〕