快排
var quickSort = function(arr) {
if (arr.length <= 1) { return arr; }
var pivotIndex = Math.floor(arr.length / 2);
var pivot = arr.splice(pivotIndex, 1)[0];
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++){
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left).concat([pivot], quickSort(right));
}
冒泡
function calc(arr) {
for(var j = 0; j < arr.length -1; j++) {
for(var i = 0; i < arr.length-1 - j; i++) {
num++
if(arr[i] > arr[i+1]) {
num = arr[i];
arr[i] = arr[i+1];
arr[i+1] = num;
}
}
}
}