The sort( ) method sorts the elements of array in place: no copy of the array is made. If sort( ) is called with no arguments, the elements of the array are arranged in alphabetical order (more precisely, the order determined by the character encoding). To do this, elements are first converted to strings, if necessary, so that they can be compared. If you want to sort the array elements in some other order, you must supply a comparison function that compares two values and returns a number indicating their relative order. The comparison function should take two arguments, a and b, and should return one of the following:
function random(m,n){ //生成一个m、n之间的整数 var i=Math.random(); return Math.round((n-m)*i+m); }
function getRandomArr(m,n,l){ //m:生成随即整数的最小值,n:生成随即整数的最大值,l:生成的数组的长度 var resultArr=[]; for(var i=0;i<l;i++){ resultArr.push(random(m,n)) } return resultArr; }
快速排序算法的实现,这个算法取自我看到的51js论坛上这个网友的实现,代码如下:
复制代码 代码如下:
function doSort(a,s,e) { if(s<e) { var pos=partition(a,s,e); doSort(a,s,pos-1); doSort(a,pos+1,e); } } function partition(a,st,en) { var s=st; var e=en+1; var temp=a[s]; while(1) { while(a[++s]<temp); while(a[--e]>temp); if(s>e)break; var tem=a[s]; a[s]=a[e]; a[e]=tem; } a[st]=a[e]; a[e]=temp; return e; }