js 手写快排

var arr = [12,7,19, 55,10,21,45,13,25 ,8];

function quickSort(arr){
    if(arr.length <= 1) {
        return arr
    };
    var mNumIndex = Math.floor(arr.length/2);
    var mNum = arr.splice([mNumIndex], 1)[0];
    var left = [];
    var right = [];
    for (var i = 0; i < arr.length; i++){
        if (arr[i] < mNum){
            left.push(arr[i])
        }else {
            right.push(arr[i])
        };
    };
    return quickSort(left).concat([mNum], quickSort(right));
}
console.log(quickSort(arr));

你可能感兴趣的:(js 手写快排)