SOURCE

const unSorted = [42, 123, 12, 3, 90, 11, 1, 56, 43, 8, 23];

function insertSort(arr) {
  for (let i = 1; i < arr.length; i++) {
    const get = arr[i]; //current
    let j = i - 1; // left hand
    while (j >= 0 && arr[j] > get) {
      arr[j + 1] = arr[j];
      j--;
    }
    arr[j + 1] = get;
  }
  return arr;
}

function binaryInsertSort(arr) {
  for (let i = 1; i < arr.length; i++) {

    let get = arr[i];
    let sortEnd = i - 1;
    let left = 0;
    let right = sortEnd;
    while (left <= right) {
      let middle = parseInt((left + right) / 2);

      if (get < arr[middle]) {
        right = middle - 1;
      } else {
        left = middle + 1;
      }
    }

    for (let j = sortEnd; j >= left; j--) {
      arr[j + 1] = arr[j]
    }
    arr[left] = get;
  }
  return arr;
}

function quickSort(arr) {
  if (arr.length === 0 || arr.length === 1) return arr;
  const middleIndex = parseInt(arr.length / 2);
  const middleValue = arr[middleIndex];
  const left = [];
  const right = [];
  for (let i = 0; i < arr.length; i++) {
    if (i === middleIndex) continue;
    if (arr[i] < middleValue) {
      left.push(arr[i]);
    }
    if (arr[i] >= middleValue) {
      right.push(arr[i]);
    }
  }
  return [...quickSort(left), middleValue, ...quickSort(right)];
}

console.log(binaryInsertSort(unSorted));
console 命令行工具 X clear

                    
>
console