YUAN 2019-06-28
插入排序是一种简单直观的排序算法。它的工作原理非常类似于我们抓扑克牌。
对于未排序的数据(右手抓到的牌),在已排序序列(左后已经排好序的牌)中从后向前扫描,找到相应位置并插入。
插入排序在实现上,通常采用in-place排序,因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。
具体算法描述如下(按从小到大排序):
具体算法描述如下(按从大到小排序):
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...
这个地址演示排序的过程非常不错,可以好好研究。