這篇文章帶給大家的內容是關於js排序 :js冒泡排序的演算法原理及程式碼實現,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
冒泡排序可謂是最經典的排序演算法了,它是基於比較的排序演算法,其優點是實現簡單,排序數量較小時性能較好。
相鄰的資料進行兩兩比較,小數放在前面,大數放在後面,如果前面的資料比後面的資料大,就交換這兩個數的位置。也可以實現大數放在前面,小數放在後面,如果前面的資料比後面的小,就交換兩個的位置。要實作上述規則需要用到兩層for迴圈。
#冒泡實作陣列從小到大排序
var arr=[2,7,1,8,6,4,3,5,9]; function mintomax(par){ var length=par.length; for(var i=0; i<length-1>par[j+1]){ var temp=par[j+1]; par[j+1]=par[j]; par[j]=temp; } } } return par; } console.log(mintomax(arr));</length-1>
冒泡實作陣列從大到小排序
var arr=[2,7,1,8,6,4,3,5,9]; function maxtomin(par){ var length=par.length; for(var i=0; i<length-1><ul class=" list-paddingleft-2"><li><p>用es6結構賦值的方式重寫上面程式碼</p></li></ul> <pre class="brush:php;toolbar:false">var arr=[2,7,1,8,6,4,3,5,9]; function mintomax(par){ var length=par.length; for(var i=0; i<length-1>par[j+1]){ [par[j],par[j+1]]=[par[j+1],par[j]] } } } return par; } console.log(mintomax(arr)); function maxtomin(par){ var length=par.length; for(var i=0; i<length-1 console.log><ul class=" list-paddingleft-2"><li><p>依照父子平鋪順序排序</p></li></ul>#<pre class="brush:php;toolbar:false">var data = [{ ZDXM_STDCODE: '100101', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1001', }, { ZDXM_STDCODE: '1001', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '', }, { ZDXM_STDCODE: '100102', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1001', }, { ZDXM_STDCODE: '100201', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1002', }, { ZDXM_STDCODE: '1002', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '', }, { ZDXM_STDCODE: '100202', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1002', }, ] function datatotree(par) { var length = par.length; for (var i = 0; i str2) { var temp = par[j + 1]; par[j + 1] = par[j] par[j] = temp; } } } return par; } var data = datatotree(data) console.log(data);
var data = [{ ZDXM_STDCODE: '100101', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1001', }, { ZDXM_STDCODE: '1001', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '', }, { ZDXM_STDCODE: '100102', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1001', }, { ZDXM_STDCODE: '100201', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1002', }, { ZDXM_STDCODE: '1002', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '', }, { ZDXM_STDCODE: '100202', ZDXM_STDNAME: '', FINA_YYSR: '', FINA_PGZHSY: '', FINA_SJZHSY: '', FINA_PGZHSYL: '', FINA_SJZHSYL: '', FINA_ZHSYLCE: '', FINA_SRJJL: '', FINA_JSSKL: '', FINA_HTE: '', GLZDXM: '1002', }, ] function datatotree(par) { var length = par.length; for (var i = 0; i str2) { [par[j],par[j+1]]=[par[j+1],par[j]]; } } } return par; } var data = datatotree(data) console.log(data);
js排序:js插入排序的演算法原理與程式碼實作############js排序動畫模擬-插入排序_javascript技巧# ##############
以上是js排序 :js冒泡排序的演算法原理及程式碼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!