1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
function selectionSort(arr) { let len = arr.length, min_index; for (let i = 0; i < len - 1; i++) { min_index = i; for (let j = i + 1; j < len; j++) { if (arr[min_index] > arr[j]) { min_index = j; } } [arr[i], arr[min_index]] = [arr[min_index], arr[i]]; } console.log(arr); return arr; }
function selectionSort1(arr) { let len = arr.length; let minIndex, temp; 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; } console.log(arr); return arr; }
selectionSort([2, 5, 9, 3, 6, 8, 7, 1]); selectionSort1([2, 5, 9, 3, 6, 8, 7, 1]);
console.log('https://www.cnblogs.com/Unknw/p/6346681.html');
|