버블 정렬
const bubble = (arr) => {
const result = [...arr];
for (let j = 0; j < result.length; j++) {
for (let i = 0; i < result.length-1; i++) {
let c = result[i + 1];
if (result[i] > result[i + 1]) {
result[i + 1] = result[i];
result[i] = c;
}
}
}
return result;
};
const arr1 = [43, 23, 74, 8, 567, 35, 1];
console.log(bubble(arr1));
선택 정렬
const select = (arr) => {
const result = [...arr];
for (let j = 0; j < result.length-1; j++) {
for (let i = j + 1; i < result.length; i++) {
let c = result[i];
if (result[j] > result[i]) {
result[i] = result[j];
result[j] = c;
}
}
}
return result;
};
const arr2 = [43, 23, 74, 8, 567, 35, 1];
console.log(select(arr2));