SOURCE

// 冒泡排序
function bbSort(arr){
    for(let i=0;i<arr.length;i++){
        for(let j=0;j<arr.length-i-1;j++){
            if(arr[j]>arr[j+1]){
                [arr[j],arr[j+1]]=[arr[j+1],arr[j]]
            }
        }
    }
    return arr
}
const a=[1,3,4,2,6,12,32,11,10,9]
// console.log(bbSort(a))

//插入排序
function insetSort(arr){
    let len =arr.length;
    let preIdex,current;
    for(let i=1;i<len;i++){
        preIdex = i-1;
        current = arr[i];
        while(preIdex>=0&&arr[preIdex]>current){
            arr[preIdex+1]=arr[preIdex]
            preIdex--
        }
        arr[preIdex+1] = current
    }
    return arr
}
const b=[6,3,2]
console.log(insetSort(b))

//选择排序
function selectSort(arr){
    for(let i=0;i<arr.length;i++){
        let min =i;
        for(let j=i+1;j<arr.length;j++){
            if(arr[j]<arr[min]){
                min =j
            }
        }
        [arr[i],arr[min]]=[arr[min],arr[i]]
    }
    return arr;
}
const c=[10,8,3,9,7,2,21,13]
console.log(selectSort(c))

//快速排序
function quickSort(arr) {
  if (arr.length <= 1) {
    return arr;
  }

  const pivot = arr[0];
  const left = [];
  const right = [];

  for (let i = 1; i < arr.length; i++) {
    if (arr[i] < pivot) {
      left.push(arr[i]);
    } else {
      right.push(arr[i]);
    }
  }

  return [...quickSort(left),pivot,...quickSort(right)];
}

// 示例用法
const unsortedArray = [5, 3, 7, 1, 9, 2, 6, 4, 8];
const sortedArray = quickSort(unsortedArray);

console 命令行工具 X clear

                    
>
console