/*采用快排的方法排序,取第一个值为轴对数组进行分割排序,不断迭代后实现数组的排序*/ //定义分割函数 function partF(A,low, high){ var temp = A[low]; while(low < high) { while(low < high && A[high] >= temp) high--; A[low] = A[high]; while(low < high && A[low] <= temp) low++; A[high] = A[low]; } A[low] = temp; return low; } //快排函数 function Qsort(A,low,high){ var tempL; if(low < high) { tempL = partF(A,low,high); Qsort(A,low,tempL-1); Qsort(A,tempL+1,high); } }
//测试代码
window.onload = function(){ var A = [2,7,8,6,1,4,3,9,5,10]; Qsort(A,0,A.length-1); console.log(A); }