插入排序是一種簡單直觀的排序算法。它的工做原理很是相似於咱們抓撲克牌。
對於未排序的數據(右手抓到的牌),在已排序序列(左後已經排好序的牌)中從後向前掃描,找到相應位置並插入。
插入排序在實現上,一般採用in-place排序,於是在從後向前掃描過程當中,須要反覆把已排序元素逐步向後挪位,爲最新元素提供插入空間。算法
具體算法描述以下(按從小到大排序):數組
具體算法描述以下(按從大到小排序):ide
function mintomax(par) { for (var i = 1; i < par.length; i++) { for (var j = i - 1; j >= 0; j--) { if (par[j + 1] < par[j]) { [par[j],par[j+1]]=[par[j+1],par[j]]; } else if (par[j + 1] >= par[j]) { break; } } } return par; } var arr = [11, 2, 3, 445, 7, 32, 71, 8, 94]; console.log(mintomax(arr));
function mintomax(par){ for(var i=1; i<par.length; i++){ j=i-1; while(j>=0 && par[j]>par[j+1]){ [par[j],par[j+1]]=[par[j+1],par[j]]; j--; } } return par; } var arr = [11, 2, 3, 445, 7, 32, 71, 8, 94]; console.log(mintomax(arr));
function maxtomin(par) { for (var i = 1; i < par.length; i++) { for (var j = i - 1; j >= 0; j--) { if (par[j + 1] > par[j]) { [par[j],par[j+1]]=[par[j+1],par[j]]; } else if (par[j + 1] <= par[j]) { break; } } } return par; } var arr = [11, 2, 3, 445, 7, 32, 71, 8, 94]; console.log(maxtomin(arr));
function maxtomin(par){ for(var i=1; i<par.length; i++){ j=i-1; while(j>=0 && par[j]<par[j+1]){ [par[j],par[j+1]]=[par[j+1],par[j]]; j--; } } return par; } var arr = [11, 2, 3, 445, 7, 32, 71, 8, 94]; console.log(maxtomin(arr));
function datatotree(par) { for (var i = 1; i < par.length; i++) { for (var j = i-1; j >=0; 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 (str2 < str1) { [par[j],par[j+1]]=[par[j+1],par[j]]; }else if(str2 >= str1){ break; } } } return par; } 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', }, ] console.log(datatotree(data));
地址:http://tools.jb51.net/aidedde...
這個地址演示排序的過程很是不錯,能夠好好研究。工具