오늘은 병합정렬과 퀵정렬에 대해서 공부해보았다
function mergedSort(arr){
let length = arr.length;
let result = [];
if (length <=1){
return arr;
}
let middleNum = parseInt(length/2);
let groupOne = mergedSort(arr.slice(0, middleNum));
let groupTwo = mergedSort(arr.slice(middleNum, ));
while(groupOne.length !== 0 && groupTwo.length !== 0){
if (groupOne[0] < groupTwo[0]) {
result.push(groupOne.shift());
} else {
result.push(groupTwo.shift())'
}
}
while(groupOne.length !== 0){
result.push(groupOne.shift());
}
while(groupTwo.length !== 0){
result.push(groupTwo.shift());
}
return result;
}
function quickSort(arr) {
let length = arr.length;
if (length <= 1) {
return arr;
}
let pivot = [arr.shift()];
let groupOne = [];
let groupTwo = [];
for (const i in arr) {
if (arr[i] < pivot) {
groupOne.push(arr[i]);
} else {
groupTwo.push(arr[i])
}
}
return quickSort(groupOne).concat(pivot, quickSort(groupTwo))
}