// 冒泡排序
function bubbleSort(arr) {
const len = arr.length;
for(let i = 0; i < len - 1; i++) {for(let j = i + 1; j < len; j++) {if(arr[i] > arr[j]) {const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
return arr;
}
const ans = bubbleSort([2,1,3]);
console.log('ans', ans)
// 快排
function quickSort(arr) {if(arr.length <= 1) return arr;
const left = [], right = [];
// 找两头的索引
const middleIndex = Math.floor(arr.length / 2);
// 取两头的 value,并删除了两头的
const middleVal = arr.splice(middleIndex, 1)[0];
for(let i = 0; i < arr.length; i++) {if(arr[i] < middleVal) {left.push(arr[i])
}else {right.push(arr[i])
}
}
// 递归 left 和 right,并加上 middleValue
return quickSort(left).concat(middleVal, quickSort(right));
}
const ans1 = quickSort([2,1,3]);
console.log('ans1', ans1)