let arr = [9,6,2,1,4,5,7,8,0,3];
function quickSortClassic(arr) {
let p = arr[0]
let left_arr = []
let right_arr = []
for (let i = 1; i < arr.length; i++) {
let temp = arr[i]
temp < p ? left_arr.push(temp) : right_arr.push(temp)
}
if (left_arr.length >= 2) {
left_arr = quickSortClassic(left_arr)
}
if (right_arr.length >= 2) {
right_arr = quickSortClassic(right_arr)
}
return left_arr.concat(p,right_arr);
}
function quickSortEs6(arr) {
if(!arr.length){
return[]
}
const [p, ...rest] = arr
return [...quickSortEs6(rest.filter(item => item < p)), p, ...quickSortEs6(rest.filter(item => item >= p))]
}
function SelectionSort(arr= [1, 2, 3, 4]) {
const len = arr.length;
for (let i = 0; i < len ; i++) {
for (let j = i; j < len; j++) {
if (arr[i] > arr [j + 1]) {
let temp = arr[i]
arr[i] = arr[ j + 1]
arr[j + 1] = temp
}
}
}
return arr
}
function bubbleSort(arr) {
let len= arr.length;
for (let i = 0; i < len ; i++) {
for (let j = 0; j < len - i; j++) {
if (arr[j] > arr [j + 1]) {
let temp = arr[j]
arr[j] = arr[ j + 1]
arr[j + 1] = temp
}
}
}
return arr
}
console.log(quickSortClassic(arr));
console.log(quickSortEs6(arr))
console.log(SelectionSort(arr))
console.log(bubbleSort(arr))
本作品採用《CC 協議》,轉載必須註明作者和本文連結