以下为ChatGPT自动生成的解答,正确与否请自行判断。

BFE.devAI解答 - JavaScript 代码题
44. 手写Selection Sort

function selectionSort(arr) {
  for(let i=0; i<arr.length-1; i++){
    let minIndex = i;
    for(let j=i+1; j<arr.length; j++){
      if(arr[j]<arr[minIndex]){
        minIndex = j;
      }
    }
    if(minIndex !== i){
      let temp = arr[i];
      arr[i] = arr[minIndex];
      arr[minIndex] = temp;
    }
  }
}

// 示例
let arr = [3, 1, 5, 6, 2, 4];
selectionSort(arr);
console.log(arr); // [1, 2, 3, 4, 5, 6]

时间复杂度为O(n^2),空间复杂度为O(1)。这是不稳定的排序,因为交换可能会破坏相同元素之间的原有顺序。