Muscardinus
선택정렬 본문
728x90
const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
function selectionSort(array) {
for (let i = 0; i < array.length; i++) {
let minIndex = i;
let temp = array[i];
for (let j = i+1; j < array.length; j++) {
if (array[minIndex] > array[j]) minIndex = j;
}
array[i] = array[minIndex];
array[minIndex] = temp;
}
return array;
}
selectionSort(numbers);
// Space Complexity : O(1)
// Time Complexity(Worst): O(n^2)
728x90
Comments