快排排序:找到一个数,数组通过这个数的大小去分左右两边,递归
function quickSort(arr) {
var len = arr.length
if(len <= 1) return arr
var temp = arr[0]
var left = []
var right = []
for(var i = 0; i < len; i++){
if(compare(temp, arr[i])){ // compare比较
left.push(arr[i])
}else{
right.push(arr[i])
}
}
return quickSort(left).concat(temp, quickSork(right))
}
冒泡排序
function bubbleSort(arr) {
var len = arr.length
var temp
if(len <= 1) return arr
for(var i = 0; i < len; i++){
for(var j = len - 1; j >= i; j--){
if(compare(arr[i], arr[j])){
temp = arr[j]
arr[j] = arr[j - 1]
arr[j - 1] = temp
}
}
}
return arr
}