1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| function quick_sort(list, start, end) {
if (start < end) {
var pivotpos = partition(list, start, end); //找出快排的基数
quick_sort(list, start, pivotpos - 1); //将左边的快排一次
quick_sort(list, pivotpos + 1, end); //将右边的快排一次
}
}
//将一个序列调整成以基数为分割的两个区域,一边全都不小于基数,一边全都不大于基数
function partition(list, start, end) {
var pivotpos = start;
var pivot = list[start];
var tmp;
for(var i = start + 1; i <= end; i ++) {
if (list[i] < pivot) {
tmp = list[i];
pivotpos += 1;
list[i] = list[pivotpos];
list[pivotpos] = tmp;
}
}
tmp = list[start];
list[start] = list[pivotpos];
list[pivotpos] = tmp;
return pivotpos;
}
var list = [8,2,4,65,2,4,7,1,9,0,2,34,12];
quick_sort(list, 0, list.length);
|