[选择排序可视化视频](https://www.reddit.com/r/programming/comments/e5md13/selection_sort_visualization/) 选择排序和插入排序有些类似,也分已排序序列和未排序序列。 但是选择排序是`将最小的元素存放在数组起始位置,再从剩下的未排序的序列中寻找最小的元素,然后将其放到已排序的序列后面`。以此类推,直到排序完成。 ```js const selectSort = function(arr) { const len = arr.length let temp, minIndex for (let i = 0; i < len - 1; i++) { minIndex = i for (let j = i + 1; j < len; j++) { if (arr[j] <= arr[minIndex]) { minIndex = j } } temp = arr[i] arr[i] = arr[minIndex] arr[minIndex] = temp } return arr } ``` - 时间复杂度: O(n^2) - 空间复杂度: O(1) - 不稳定
Activity