-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ_DuplicateSort.js
32 lines (30 loc) · 1.04 KB
/
BOJ_DuplicateSort.js
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
/**
* @desc problem : 중복 빼고 정렬하기
* @desc site : Baekjoon Onlie Judge
* @desc level: S5
* @desc url : https://www.acmicpc.net/problem/10867
*/
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
// const input = fs.readFileSync("../temp.txt").toString().trim().split("\n");
let answer = '';
const inputArray = input.slice(1).map((item) => Number(item));
console.log(quickSort(inputArray).join('\n'));
function quickSort(array) {
if (array.length <= 1) {
return array;
}
//배열의 마지막 인덱스를 pivot으로 설정
const pivot = array[array.length - 1];
const leftArr = [];
const rightArr = [];
//복사한 배열을 loop -> pivot보다 작으면 leftArray, 크면 rightArray
for (const item of array.slice(0, array.length - 1)) {
if (item < pivot) {
leftArr.push(item);
} else if (item > pivot) {
rightArr.push(item);
}
}
return [...quickSort(leftArr), pivot, ...quickSort(rightArr)];
}