快速排序算法思想及视频演示: http://www.tyut.edu.cn/kecheng1/site01/suanfayanshi/quick_sort.asp
function quicksort(arr){
var temparr = arr;
var arrlen = temparr.length-1;
subquicksort(arr,0,arrlen);
alert(arr);
}
// low <= high
function subquicksort(arr,low,high){
var v = arr[low];
var j = low + 1;
var k = high;
if(low >= high) {
return;
}
var protect = 100;
while(1 && protect-- >0)
{
for(; j<=high; j++) {
if(arr[j]>=v) {
break;
}
}
// end with low + 1 j-1 <v, arr j >= v
for(; k>low; k--){
if(arr[k] < v){
break;
}
}
// end with k+1 high >= v, arr k < v
if (j < k) {
var temp = arr[k];
arr[k] = arr[j];
arr[j] = temp;
} else {
break;
}
// end with low j, k, high
j++;
}
// k < v
arr[low] = arr[k];
arr[k] = v;
subquicksort(arr,low,k-1);
subquicksort(arr,k+1,high);
}
var arrstr = [12,97,45,23,4,67,8,2,34];
quicksort(arrstr);