SOURCE

function selectionSort(arr) {
  for(let i = 0; i < arr.length - 1; i++) {
    let min = arr[i];
    let index = i;
    for(let j = i + 1; j < arr.length; j++) {
      if(min > arr[j]) {
        min = arr[j];
        index = j;
      }
    }
    let temp = arr[i];
    console.log(`第${i+1}轮比较,交换${arr[i]}和${arr[index]}`)
    console.log(arr);
    arr[i] = min;
    arr[index] = temp;
    console.log(arr);
  }
  return arr;
}

const testArr = [5,3,4,7,0,9,1,6,2,8];
console.log(selectionSort(testArr))
console 命令行工具 X clear

                    
>
console