标签:new color erro sort should throw script while function
基本过程
1. 选取数组中的一个元素作为基准(pivot)
2. 按照基准将数组分区,左区全部小于基准,右区全部大于基准,使用方法为原地置换(swap in place)
3. 对左右分区递归使用1和2步,直至左右分区只有一个或零个元素,排序完成
JavaScript实现
function fQuickSort(arr,low,high) { if(low >= high){ throw new Error(‘low should less than high‘); } var pivotIndex = fSwapInPlace(arr,low,high); fQuickSort(arr,low,pivotIndex-1); fQuickSort(arr,pivotIndex+1,high); } function fSwapInPlace(arr,low,high){ var pivot = arr[low]; while (low < high) { while(low < high && arr[high] > pivot){ high--; } arr[low] = arr[high]; while(low < high && arr[low] <= pivot){ low++; } arr[high] = arr[low]; } arr[low] = pivot; //stop at pivot place,low equals pivot index return low; }
标签:new color erro sort should throw script while function
原文地址:http://www.cnblogs.com/mengff/p/6152543.html