冒泡排序可謂是最經典的排序算法了,它是基於比較的排序算法,其優勢是實現簡單,排序數量較小時性能較好。es6
相鄰的數據進行兩兩比較,小數放在前面,大數放在後面,若是前面的數據比後面的數據大,就交換這兩個數的位置。也能夠實現大數放在前面,小數放在後面,若是前面的數據比後面的小,就交換兩個的位置。要實現上述規則須要用到兩層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; i++){ for(var j=0; j<length-i-1; j++){ if(par[j]>par[j+1]){ var temp=par[j+1]; par[j+1]=par[j]; par[j]=temp; } } } return par; } console.log(mintomax(arr));
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; i++){ for(var j=0; j<length-i-1; j++){ if(par[j]<par[j+1]){ var temp=par[j+1]; par[j+1]=par[j]; par[j]=temp; } } } return par; } console.log(maxtomin(arr));
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; i++){ for(var j=0; j<length-i-1; j++){ if(par[j]>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; i++){ for(var j=0; j<length-i-1; j++){ if(par[j]<par[j+1]){ [par[j],par[j+1]]=[par[j+1],par[j]] } } } return par; } console.log(maxtomin(arr));
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 < length-1; i++) { for (var j = 0; j < length - i - 1; j++) { var str1=par[j].GLZDXM+par[j].ZDXM_STDCODE; var str2=par[j+1].GLZDXM+par[j+1].ZDXM_STDCODE; if(par[j].GLZDXM==null){ str1=par[j].ZDXM_STDCODE; } if(data[j+1].GLZDXM==null){ str2=par[j+1].ZDXM_STDCODE; } if (str1 > 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 < length-1; i++) { for (var j = 0; j < length - i - 1; j++) { var str1=par[j].GLZDXM+par[j].ZDXM_STDCODE; var str2=par[j+1].GLZDXM+par[j+1].ZDXM_STDCODE; if(par[j].GLZDXM==null){ str1=par[j].ZDXM_STDCODE; } if(data[j+1].GLZDXM==null){ str2=par[j+1].ZDXM_STDCODE; } if (str1 > str2) { [par[j],par[j+1]]=[par[j+1],par[j]]; } } } return par; } var data = datatotree(data) console.log(data);