Skip to content

Commit 7e678a0

Browse files
committed
Solve : 045번, 050~052번 문제 해결
1 parent 45f09b9 commit 7e678a0

File tree

8 files changed

+233
-0
lines changed

8 files changed

+233
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 문제45 : getTime()함수 사용하기
2+
3+
Date객체의 메소드 중 하나인 getTime()은 1970년 1월 1일 0시 0분 0초 이후로부터 지금까지 흐른 시간을 천분의 1초 단위(ms)로 반환합니다.
4+
5+
이를 이용하여 **현재 연도(2019)를 출력해보세요.**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
# 문제45 : getTime()함수 사용하기
3+
4+
Date객체의 메소드 중 하나인 getTime()은 1970년 1월 1일 0시 0분 0초 이후로부터 지금까지 흐른 시간을 천분의 1초 단위(ms)로 반환합니다.
5+
6+
이를 이용하여 **현재 연도(2019)를 출력해보세요.**
7+
*/
8+
9+
const time = new Date();
10+
console.log(time.getTime());
11+
console.log(Math.floor(time.getTime() / (60 * 60 * 24 * 365 * 1000)) + 1970);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 문제50 : 버블정렬 구현하기
2+
3+
버블정렬은 두 인접한 원소를 검사하여 정렬하는 방법을 말합니다. 시간 복잡도는 느리지만 코드가 단순하기 때문에 자주 사용됩니다.
4+
5+
아래 코드의 빈 칸을 채워 버블 정렬을 완성해 봅시다.
6+
7+
```jsx
8+
function bubble(arr) {
9+
let result = arr.slice();
10+
11+
for (let i = 0; i < result.length - 1; i++) {
12+
for (/*빈칸을 채워주세요.*/) {
13+
if (result[j] > result[j + 1]) {
14+
//빈칸을 채워주세요.
15+
}
16+
}
17+
}
18+
return result;
19+
}
20+
21+
const items = prompt('입력해주세요.').split(' ').map((n) => {
22+
return parseInt(n, 10);
23+
});
24+
25+
console.log(bubble(items));
26+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
# 문제50 : 버블정렬 구현하기
3+
4+
버블정렬은 두 인접한 원소를 검사하여 정렬하는 방법을 말합니다. 시간 복잡도는 느리지만 코드가 단순하기 때문에 자주 사용됩니다.
5+
6+
7+
아래 코드의 빈 칸을 채워 버블 정렬을 완성해 봅시다.
8+
*/
9+
10+
function bubble(arr) {
11+
let result = arr.slice();
12+
13+
for (let i = 0; i < result.length - 1; i++) {
14+
for (let j = 0; j < result.length - i; j++) {
15+
if (result[j] > result[j + 1]) {
16+
let value = result[j];
17+
result[j] = result[j + 1];
18+
result[j + 1] = value;
19+
}
20+
}
21+
}
22+
return result;
23+
}
24+
25+
const items = prompt("입력해주세요.")
26+
.split(" ")
27+
.map((n) => {
28+
return parseInt(n, 10);
29+
});
30+
31+
console.log(bubble(items));
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 문제51 : merge sort를 만들어보자
2+
3+
병합정렬(merge sort)은 대표적인 정렬 알고리즘 중 하나로 다음과 같이 동작합니다.
4+
5+
> 1. 리스트의 길이가 0 또는 1이면 이미 정렬된 것으로 본다. 그렇지 않은 경우에는
6+
> 2. 정렬되지 않은 리스트를 절반으로 잘라 비슷한 크기의 두 부분 리스트로 나눈다.
7+
> 3. 각 부분 리스트를 재귀적으로 합병 정렬을 이용해 정렬한다.
8+
> 4. 두 부분 리스트를 다시 하나의 정렬된 리스트로 합병한다.
9+
10+
출처 : 위키피디아
11+
12+
다음 코드의 빈칸을 채워 병합정렬을 완성해 봅시다.
13+
14+
```jsx
15+
function mergeSort(arr){
16+
if (arr.length <= 1){
17+
return arr;
18+
}
19+
20+
const mid = Math.floor(arr.length / 2);
21+
const left = arr.slice(0,mid);
22+
const right = arr.slice(mid);
23+
24+
return merge(mergeSort(left), mergeSort(right));
25+
}
26+
27+
function merge(left, right){
28+
let result = [];
29+
30+
while (/*빈칸을 채워주세요*/ && /*빈칸을 채워주세요*/){
31+
if (/*빈칸을 채워주세요*/){
32+
result.push(left.shift());
33+
} else {
34+
result.push(right.shift());
35+
}
36+
}
37+
while (left.length) {
38+
/*빈칸을 채워주세요*/
39+
}
40+
while (right.length) {
41+
/*빈칸을 채워주세요*/
42+
}
43+
44+
return result;
45+
}
46+
47+
const array = prompt('배열을 입력하세요').split(' ').map(n => parseInt(n, 10));
48+
49+
console.log(mergeSort(array));
50+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
# 문제51 : merge sort를 만들어보자
3+
4+
병합정렬(merge sort)은 대표적인 정렬 알고리즘 중 하나로 다음과 같이 동작합니다.
5+
6+
> 1. 리스트의 길이가 0 또는 1이면 이미 정렬된 것으로 본다. 그렇지 않은 경우에는
7+
> 2. 정렬되지 않은 리스트를 절반으로 잘라 비슷한 크기의 두 부분 리스트로 나눈다.
8+
> 3. 각 부분 리스트를 재귀적으로 합병 정렬을 이용해 정렬한다.
9+
> 4. 두 부분 리스트를 다시 하나의 정렬된 리스트로 합병한다.
10+
11+
출처 : 위키피디아
12+
13+
다음 코드의 빈칸을 채워 병합정렬을 완성해 봅시다.
14+
*/
15+
16+
function mergeSort(arr) {
17+
if (arr.length <= 1) {
18+
return arr;
19+
}
20+
21+
const mid = Math.floor(arr.length / 2);
22+
const left = arr.slice(0, mid);
23+
const right = arr.slice(mid);
24+
25+
return merge(mergeSort(left), mergeSort(right));
26+
}
27+
28+
function merge(left, right) {
29+
let result = [];
30+
31+
while (left.length && right.length) {
32+
if (left[0] < right[0]) {
33+
result.push(left.shift());
34+
} else {
35+
result.push(right.shift());
36+
}
37+
}
38+
while (left.length) {
39+
result.push(left.shift());
40+
}
41+
while (right.length) {
42+
result.push(right.shift());
43+
}
44+
return result;
45+
}
46+
47+
const array = prompt("배열을 입력하세요")
48+
.split(" ")
49+
.map((n) => parseInt(n, 10));
50+
51+
console.log(mergeSort(array));
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 문제52 : quick sort
2+
3+
다음 빈 칸을 채워 퀵 정렬을 완성해주세요.
4+
5+
```jsx
6+
function quickSort(arr){
7+
if (arr.length <= 1){
8+
return arr;
9+
}
10+
11+
const pivot = arr[0];
12+
const left = [];
13+
const right = [];
14+
15+
for (let i=1; i<arr.length; i++){
16+
if(/*빈칸을 채워주세요*/){
17+
left.push(arr[i]);
18+
} else {
19+
right.push(arr[i]);
20+
}
21+
}
22+
return /*빈칸을 채워주세요*/
23+
}
24+
25+
const array = prompt('배열을 입력하세요').split(' ').map(n => parseInt(n, 10));
26+
27+
console.log(quickSort(array));
28+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
# 문제52 : quick sort
3+
4+
다음 빈 칸을 채워 퀵 정렬을 완성해주세요.
5+
*/
6+
7+
function quickSort(arr) {
8+
if (arr.length <= 1) {
9+
return arr;
10+
}
11+
12+
const pivot = arr[0];
13+
const left = [];
14+
const right = [];
15+
16+
for (let i = 1; i < arr.length; i++) {
17+
if (arr[i] < pivot) {
18+
left.push(arr[i]);
19+
} else {
20+
right.push(arr[i]);
21+
}
22+
}
23+
// return quickSort(left) + pivot + quickSort(right);
24+
return quickSort(left).concat(pivot, quickSort(right));
25+
}
26+
27+
const array = prompt("배열을 입력하세요")
28+
.split(" ")
29+
.map((n) => parseInt(n, 10));
30+
31+
console.log(quickSort(array));

0 commit comments

Comments
 (0)