// 选择排序算法
// 选择排序算法是一种原地址比较排序算法。
// 选择排序大体的思路是找数据结构中的最小值并将其放置在第一位。找到第二小的的值放置的第二类,以此类推数据结构
// 每一次内循环遍历寻找最小数,记录下minIndex,并在此次内循环后交换minIndex和i的位置code
function swap(arr, indexA, indexB) {排序
return [arr[indexA], arr[indexB]] = [arr[indexB], arr[indexA]];
}io
function selectionSort(arr) {console
let len = arr.length; for (let i = 0; i < len; i++) { let minIndex = i; for (let j = i + 1; j < len; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } if (i != minIndex ) { swap(arr, i, minIndex); } } return arr;
}function
const arr = [91, 60, 96, 7, 35, 65, 10, 65, 9, 30, 20, 31, 77, 81, 24];
console.log(selectionSort(arr));select