最近编写了快速冒泡排序代码,帮忙看看有没有什么破绽?
来源:互联网 发布:电音用什么软件 编辑:程序博客网 时间:2024/04/29 13:32
//快速冒泡排序function quickBubbleSort(arrDt,cmp){ for(var i=1;i<arrDt.length;i++){ quickBubbleSortAction(arrDt,cmp,i); } return arrDt; }function quickBubbleSortAction(arrDt,cmp,length){ var nxtDt=arrDt[length]; //检查头 var def=cmp.compere(arrDt[0],nxtDt); if(def>=0){ //插入首位 quickBubbleSort_insert(arrDt,length,0,nxtDt); return; } if(length==1){ //第一和第二个比较 return; } //检查尾 var end=length-1; def=cmp.compere(arrDt[end],nxtDt); if(def<=0)return; //比最后1个大 var start=0; var mid; while(start<end-1){ mid=(start+end)/2; def=cmp.compere(arrDt[mid],nxtDt); if(def==0){ end=mid; break; //插入相等的下一位 } if(def<0){ start=mid; }else{ end=mid; } } quickBubbleSort_insert(arrDt,length,end,nxtDt); }function quickBubbleSort_insert(arrDt,length,pos,nxtDt){ //移位 北漂族:www.360lifer.com for(var i=length;i>pos;i--){ arrDt[i]=arrDt[i-1]; } arrDt[pos]=nxtDt;}//数值字段比较对象function QBSNumComperer(cpFldNm,isDesc){ this.cpFldNm=cpFldNm; if(isDesc){ this.isDesc=true; }else{ this.isDesc=false; } this.compere=function(it1,it2){ if(this.isDesc){ return it2[this.cpFldNm]-it1[this.cpFldNm]; }else{ return it1[this.cpFldNm]-it2[this.cpFldNm]; } }}