function swap(arr, target1, target2) {
const temp = arr[target1]
arr[target1] = arr[target2]
arr[target2] = temp
}
// 오름차순
function ascSelectionSort(arr) {
arr.map((_, idx) => {
let minIdx = idx
arr.slice(idx + 1).map((val, i) => {
if (val < arr[minIdx]) {
minIdx = i + idx + 1
}
})
swap(arr, idx, minIdx)
})
return arr
}
//내림차순
function descSelectionSort(arr) {
arr.map((_, idx) => {
let maxIdx = idx
arr.slice(idx + 1).map((val, i) => {
if (val > arr[maxIdx]) {
maxIdx = i + idx + 1
}
})
swap(arr, idx, maxIdx)
})
return arr
}
ascSelectionSort([5, 1, 4, 7, 2, 6, 8, 3]) // [1, 2, 3, 4, 5, 6, 7, 8]
descSelectionSort([5, 1, 4, 7, 2, 6, 8, 3]) // [ 8, 7, 6, 5, 4, 3, 2, 1 ]