冒泡排序,快速排序,插入排序(JS版)

//冒泡排序 function sortBubble(array){ var len=array.length,i,j,tmp; for(i=len-1;i>=1;i--){ for(j=0;j<=i-1;j++){ if(array[j]>array[j+1]){ d=array[j+1]; array[j+1]=array[j]; array[j]=d; } } } return array; } //快速排序 function sortQuick(array){ var low=0,high=array.length-1; var sort=function(low,high){ if(low==high){ return; } var key=array[low]; var tmplow=low; var tmphigh=high; while(low<high){ while(low<high&&key<=array[high]){ --high; } array[low]=array[high]; while(low<high&&array[low]<=key) { ++low; } array[high]=array[low]; if(low==tmplow){ sort(++low,tmphigh); return; } }; array[low]=key; sort(tmplow,low-1); sort(high+1,tmphigh); }; sort(low,high); sort(); return array; } //插入排序 function sortInsert(array){ var i=1,j,len=array.length,key; for(;i<len;i++){ j=i; key=array[j]; while(--j>-1){ if(array[j]>key){ array[j+1]=array[j]; } else { break; } } array[j+1]=key; } return array; } 

你可能感兴趣的:(function)