Skip to content

[3주차] 정렬 (버블, 선택, 삽입, 셀, 머지, 퀵, 힙) #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added problem-1/01-bubble-sort.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions problem-1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
```

2. 버블 정렬의 코드를 보고 어떻게 동작하는지 그림을 그려보세요. 마찬가지로 위의 값이 주어졌다고 했을 때 코드를 따라가며 그림을 그려가며 이해해 보세요.
![01-bubble-sort.png](01-bubble-sort.png)


3. 버블 정렬의 코드를 직접 구현해 주세요.

Expand Down
22 changes: 22 additions & 0 deletions problem-1/problem-1.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
const exchange = (array, index1, index2) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 일은 swap이라는 말을 더 많이 사용하는 것 같네요.

[array[index1], array[index2]] = [array[index2], array[index1]];
};

const isFirstBiggerThanSecond = (a, b) => a > b;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수가 하는 일을 굉장히 자세하게 작성하셨네요. 아마도 각 파라미터가 무엇을 의미하는지까지 명시해주고 싶으셨던 것 같습니다. isGreaterThen 정도도 괜찮을 것 같네요.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아마도 각 파라미터가 무엇을 의미하는지까지 명시해주고 싶으셨던 것 같습니다.

정확히 제가 의도한 바가 맞습니다. ㅎㅎ
함수명이 너무 길어지는 것 같긴하지만 나중에 제 코드를 제가 다시 읽을 때도
무슨 의미였는지 잘 기억나는 효과가 있어서 이런 식으로 작성해보려고 합니다.

const bubbleSort = (array) => {
const { length } = array;

for (let pass = 1; pass <= length; pass++) {
let exchangeCount = 0;

for (let index = 0; index < length - pass; index++) {
if (isFirstBiggerThanSecond(array[index], array[index + 1])) {
exchange(array, index, index + 1);
exchangeCount++;
}
}

// todo : 정렬이 완료됐다면 loop 를 종료한다.
if (exchangeCount === 0) {
break;
}
}
};

test.each([
Expand Down
Binary file added problem-2/02-selection-sort.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions problem-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
```

2. 선택 정렬의 코드를 보고 어떻게 동작하는지 그림을 그려보세요. 마찬가지로 위의 값이 주어졌다고 했을 때 코드를 따라가며 그림을 그려가며 이해해 보세요.
![02-selection-sort.jpeg](02-selection-sort.jpeg)


3. 선택 정렬의 코드를 직접 구현해 주세요.
23 changes: 23 additions & 0 deletions problem-2/problem-2.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
const exchange = (array, index1, index2) => {
[array[index1], array[index2]] = [array[index2], array[index1]];
};

const isFirstLesserThanSecond = (a, b) => a < b;

const findMinValueIndex = (array, startIndex) => {
let minValueIndex = startIndex;

for (let index = startIndex + 1; index < array.length; index++) {
if (isFirstLesserThanSecond(array[index], array[minValueIndex])) {
minValueIndex = index;
}
}

return minValueIndex;
};

const selectionSort = (array) => {
for (let index = 0; index < array.length - 1; index++) {
const minValueIndex = findMinValueIndex(array, index);

exchange(array, index, minValueIndex);
}
};

test.each([
Expand Down
Binary file added problem-3/03-insertion-sort.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions problem-3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
```

2. 삽입 정렬의 코드를 보고 어떻게 동작하는지 그림을 그려보세요. 마찬가지로 위의 값이 주어졌다고 했을 때 코드를 따라가며 그림을 그려가며 이해해 보세요.
![03-insertion-sort.png](03-insertion-sort.png)


3. 삽입 정렬의 코드를 직접 구현해 주세요.
15 changes: 15 additions & 0 deletions problem-3/problem-3.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
const exchange = (array, index1, index2) => {
[array[index1], array[index2]] = [array[index2], array[index1]];
};

const isFirstLesserThanSecond = (a, b) => a < b;
const insertionSort = (array) => {
for (let currentIndex = 1; currentIndex < array.length; currentIndex++) {
for (let comparisonIndex = currentIndex; comparisonIndex > 0; comparisonIndex--) {
if (isFirstLesserThanSecond(array[comparisonIndex], array[comparisonIndex - 1])) {
exchange(array, comparisonIndex, comparisonIndex - 1);
} else {
// todo: 비교했을 때 왼쪽보다 작지 않다면 비교를 중지하고, 다음 index 로 넘어간다.
break;
}
}
}
};

test.each([
Expand Down
Binary file added problem-4/04-shell-sort.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions problem-4/problem-4.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
const exchange = (array, index1, index2) => {
[array[index1], array[index2]] = [array[index2], array[index1]];
};

const isFirstLesserThanSecond = (a, b) => a < b;

const shellSort = (array) => {
const { length } = array;

let gap = Math.floor(length / 2);

while (gap > 0) {
for (let index = gap; index < length; index++) {
for (let previousIndex = index; previousIndex >= 0; previousIndex = previousIndex - gap) {
if (isFirstLesserThanSecond(array[previousIndex], array[previousIndex - gap])) {
exchange(array, previousIndex, previousIndex - gap);
} else {
break;
}
}
}

gap = Math.floor(gap / 2);
}
};

test.each([
Expand Down
Binary file added problem-5/05-merge-sort-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-5/06-merge-sort-02.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion problem-5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
```

2. 머지 정렬의 코드를 보고 어떻게 동작하는지 그림을 그려보세요. 마찬가지로 위의 값이 주어졌다고 했을 때 코드를 따라가며 그림을 그려가며 이해해 보세요.

![05-merge-sort-01.png](05-merge-sort-01.png)
![06-merge-sort-02.jpeg](06-merge-sort-02.jpeg)
3. 머지 정렬의 코드를 직접 구현해 주세요.
29 changes: 14 additions & 15 deletions problem-5/merge-sort-bottomUp.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
const less = (a, b) => a < b;

const mergeSortBottomUp = (list) => {
const { length } = list;

for (let n = 1; n < length; n = n * 2) {
for (let start = 0; start < length - n; start = start + n * 2) {
merge(
list,
start,
start + (n - 1),
Math.min(start + ((n * 2) - 1), length - 1),
);
}
}
};

const merge = (list, start, mid, end) => {
let left = start;
let right = mid + 1;
Expand All @@ -37,6 +22,20 @@ const merge = (list, start, mid, end) => {
}
}
};
const mergeSortBottomUp = (list) => {
const { length } = list;

for (let n = 1; n < length; n = n * 2) {
for (let start = 0; start < length - n; start = start + n * 2) {
merge(
list,
start,
start + (n - 1),
Math.min(start + ((n * 2) - 1), length - 1),
);
}
}
};

module.exports = {
mergeSortBottomUp,
Expand Down
23 changes: 11 additions & 12 deletions problem-5/merge-sort.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
const less = (a, b) => a < b;

const mergeSort = (list, start = 0, end = list.length - 1) => {
if (start >= end) {
return;
}

const mid = Math.floor((start + end) / 2);

mergeSort(list, start, mid);
mergeSort(list, mid + 1, end);
merge(list, start, mid, end);
};

const merge = (list, start, mid, end) => {
let left = start;
let right = mid + 1;
Expand All @@ -34,6 +22,17 @@ const merge = (list, start, mid, end) => {
}
}
};
const mergeSort = (list, start = 0, end = list.length - 1) => {
if (start >= end) {
return;
}

const mid = Math.floor((start + end) / 2);

mergeSort(list, start, mid);
mergeSort(list, mid + 1, end);
merge(list, start, mid, end);
};

module.exports = {
mergeSort,
Expand Down
37 changes: 36 additions & 1 deletion problem-5/problem-5.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
const mergeSort = (array) => {
const isFirstLesserThanSecond = (a, b) => a < b;
const merge = (list, start, mid, end) => {
let left = start;
let right = mid + 1;

const temp = [...list];

for (let index = start; index <= end; index++) {
if (left > mid) { // 왼쪽의 정렬이 모두 끝났는가? -> 오른쪽 항목들을 채운다.
list[index] = temp[right];
right++;
} else if (right > end) { // 오른쪽의 정렬이 모두 끝났는가? -> 왼쪽 항목들을 채운다.
list[index] = temp[left];
left++;
} else if (isFirstLesserThanSecond(temp[left], temp[right])) {
// 왼쪽과 오른쪽의 항목 중 왼쪽 값이 더 작으면 넣는다.
list[index] = temp[left];
left++;
} else {
// 왼쪽과 오른쪽의 항목 중 오른쪽 값이 더 작으면 넣는다.
list[index] = temp[right];
right++;
}
}
};

const mergeSort = (list, start = 0, end = list.length - 1) => {
if (start >= end) {
return;
}

const mid = Math.floor((start + end) / 2);

mergeSort(list, start, mid); // 절반으로 나눈 왼쪽 리스트를 정렬
mergeSort(list, mid + 1, end); // 절반으로 나눈 오른쪽 리스트를 정렬
merge(list, start, mid, end);
};

test.each([
Expand Down
8 changes: 8 additions & 0 deletions problem-6/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
```

2. 퀵 정렬의 코드를 보고 어떻게 동작하는지 그림을 그려보세요. 마찬가지로 위의 값이 주어졌다고 했을 때 코드를 따라가며 그림을 그려가며 이해해 보세요.
![quick-sort-code-01.png](quick-sort-code-01.png)
![quick-sort-code-02.png](quick-sort-code-02.png)
![quick-sort-code-03.png](quick-sort-code-03.png)
![quick-sort-code-04.png](quick-sort-code-04.png)
![quick-sort-code-05.png](quick-sort-code-05.png)
![quick-sort-code-06.png](quick-sort-code-06.png)
![quick-sort-code-07.png](quick-sort-code-07.png)
![quick-sort-code-08.png](quick-sort-code-08.png)

3. 퀵 정렬의 코드를 직접 구현해 주세요.
61 changes: 61 additions & 0 deletions problem-6/problem-6.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,65 @@
const isFirstLesserThanSecond = (a, b) => a < b;

const exchange = (array, index1, index2) => {
[array[index1], array[index2]] = [array[index2], array[index1]];
};
const shuffle = (array) => {
let randomIndex;

for (let i = array.length; i > 0; i--) {
randomIndex = Math.floor(Math.random() * i);
i--;

[array[i], array[randomIndex]] = [array[randomIndex], array[i]];
}
};

const partition = (array, low, high) => {
let left = low + 1;
let right = high;

const pivot = array[low];

while (true) {
while (isFirstLesserThanSecond(array[left], pivot)) {
if (left === high) {
break;
}

left++;
}

while (isFirstLesserThanSecond(pivot, array[right])) {
if (right === low) {
break;
}

right--;
}

if (left >= right) {
break;
}

exchange(array, left, right);
}

exchange(array, low, right);
return right;
};
const sort = (array, low, high) => {
if (low >= high) {
return;
}

const partitionIndex = partition(array, low, high);
sort(array, low, partitionIndex - 1);
sort(array, partitionIndex + 1, high);
};

const quickSort = (array) => {
shuffle(array);
sort(array, 0, array.length - 1);
};

test.each([
Expand Down
Binary file added problem-6/quick-sort-code-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-6/quick-sort-code-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-6/quick-sort-code-03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-6/quick-sort-code-04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-6/quick-sort-code-05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-6/quick-sort-code-06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-6/quick-sort-code-07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-6/quick-sort-code-08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions problem-7/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,14 @@
```

2. 힙 정렬의 코드를 보고 어떻게 동작하는지 그림을 그려보세요. 마찬가지로 위의 값이 주어졌다고 했을 때 코드를 따라가며 그림을 그려가며 이해해 보세요.
![heap-sort.png](heap-sort.png)
![heap-sort-sink-01.png](heap-sort-sink-01.png)
![heap-sort-sink-02.png](heap-sort-sink-02.png)
![heap-sort-sink-03-01.png](heap-sort-sink-03-01.png)
![heap-sort-sink-03-02.png](heap-sort-sink-03-02.png)
![heap-sort-sink-04-01.png](heap-sort-sink-04-01.png)
![heap-sort-sink-04-02.png](heap-sort-sink-04-02.png)
![heap-sort-sink-04-03.png](heap-sort-sink-04-03.png)
![heap-sort-sink-05.png](heap-sort-sink-05.png)

3. 힙 정렬의 코드를 직접 구현해 주세요.
Binary file added problem-7/heap-sort-sink-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-7/heap-sort-sink-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-7/heap-sort-sink-03-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-7/heap-sort-sink-03-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-7/heap-sort-sink-04-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-7/heap-sort-sink-04-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-7/heap-sort-sink-04-03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-7/heap-sort-sink-05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added problem-7/heap-sort.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions problem-7/problem-7.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
const exchange = (array, a, b) => {
[array[a], array[b]] = [array[b], array[a]];
};

const less = (a, b) => a < b;

const sink = (array, i, N) => {
while (2 * i <= N) {
let j = 2 * i;
if (j < N && less(array[j], array[j + 1])) {
j++;
}

if (!less(array[i], array[j])) {
break;
}

exchange(array, i, j);
i = j;
}
};

const heapSort = (array) => {
let N = array.length - 1;

for (let i = Math.floor(N / 2); i >= 1; i--) {
sink(array, i, N);
}

while (N > 1) {
exchange(array, 1, N);
N--;
sink(array, 1, N);
}
};

test.each([
Expand Down