Skip to content
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/3way-quick-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-1/3way-quick-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-1/3way-quick-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-1/3way-quick-04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions problem-1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@

- 1-1 다음 문자열이 주어졌을 때 LSD 문자열 정렬로
정렬을 할 때 동작 과정을 각각 그려주세요.
![lsd-01.png](lsd-01.png)

![lsd-02.png](lsd-02.png)

![lsd-03.png](lsd-03.png)

![lsd-04.png](lsd-04.png)

- 1-2 그다음에는 내가 직접 코드를 실행한다고 생각하면서 읽어 보세요. 아래
데이터를 넣었을 때 실제로 실행되는 과정을 그림으로 그려주세요.
![lsd-code-01.png](lsd-code-01.png)

![lsd-code-02.png](lsd-code-02.png)

![lsd-code-03.png](lsd-code-03.png)

- 1-3 앞의 과정을 생각하면서 코드를 직접 구현해 주세요.

```
Expand All @@ -16,8 +30,22 @@ BCDF

- 2-1다음 문자열이 주어졌을 때 MSD 문자열 정렬로
정렬을 할 때 동작 과정을 각각 그려주세요
![msd-01.png](msd-01.png)

![msd-02.png](msd-02.png)

![msd-03.png](msd-03.png)

- 2-2 그다음에는 내가 직접 코드를 실행한다고 생각하면서 읽어 보세요. 아래
데이터를 넣었을 때 실제로 실행되는 과정을 그림으로 그려주세요.

![msd-code-01.png](msd-code-01.png)

![msd-code-02.png](msd-code-02.png)

![msd-code-03.png](msd-code-03.png)


- 2-3 앞의 과정을 생각하면서 코드를 직접 구현해 주세요.

```
Expand All @@ -34,6 +62,14 @@ CAT
데이터를 넣었을 때 실제로 실행되는 과정을 그림으로 그려주세요.
- 3-3 앞의 과정을 생각하면서 코드를 직접 구현해 주세요.

![3way-quick-01.png](3way-quick-01.png)

![3way-quick-02.png](3way-quick-02.png)

![3way-quick-03.png](3way-quick-03.png)

![3way-quick-04.png](3way-quick-04.png)

```
BOAT
CAR
Expand Down
Binary file added problem-1/lsd-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-1/lsd-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-1/lsd-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-1/lsd-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-1/lsd-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-1/lsd-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-1/lsd-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-1/msd-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-1/msd-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-1/msd-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-1/msd-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-1/msd-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-1/msd-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.
29 changes: 28 additions & 1 deletion problem-1/sortLSD.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
const sortLSD = (words, w = words.length) => {
const sortLSD = (words, rangeToSort = words.length) => {
const numberOfWords = words.length;
const rangeOfChars = 256; // ASCII
const temp = [];

for (let d = rangeToSort - 1; d >= 0; d--) {
const frequencyOfChar = Array.from({ length: rangeOfChars + 1 }, () => 0);

// todo: 현재 문자열의 각 문자마다 ASCII 코드를 구하여 등장 빈도수를 계산한다.
for (let i = 0; i < numberOfWords; i++) {
frequencyOfChar[words[i].charCodeAt(d) + 1]++;
}

// todo: 각 문자가 처음 등장하는 인덱스를 구한다.
for (let i = 0; i < rangeOfChars; i++) {
frequencyOfChar[i + 1] += frequencyOfChar[i];
}

// todo: 임시 배열에 문자열을 정렬한다.
for (let i = 0; i < numberOfWords; i++) {
temp[frequencyOfChar[words[i].charCodeAt(d)]++] = words[i];
}

// todo: 임시 배열에 정렬된 것처럼 원본 배열도 정렬한다.
for (let i = 0; i < numberOfWords; i++) {
words[i] = temp[i];
}
}
};

test('sortLSD는 문자열을 정렬한다.', () => {
Expand Down
35 changes: 33 additions & 2 deletions problem-1/sortMSD.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,43 @@ const insertionSort = (list, lo, hi, d) => {
}
};

const R = 256;
const rangeOfChars = 256;
const CUTOFF = 15;

const charAt = (s, d) => (d < s.length ? s.charCodeAt(d) : -1);

const sort = (words, aux, lo, hi, d) => {
const sort = (words, temp, low, high, d) => {
if (high <= low + CUTOFF) {
insertionSort(words, low, high, d);
return;
}

// todo: 특정 문자의 등장 빈도를 계산하기 위한 배열 생성 (값은 모두 0으로)
const frequencyOfChars = Array.from({ length: rangeOfChars + 2 }, () => 0);

// todo: 해당 문자의 ASCII 값으로 등장 빈도수 계산하기
for (let i = low; i <= high; i++) {
frequencyOfChars[charAt(words[i], d) + 2]++;
}

// todo: 배열 안의 값을 누적하면서 각 문자가 처음 등장하는 인덱스 구하기
for (let i = 0; i < rangeOfChars + 1; i++) {
frequencyOfChars[i + 1] += frequencyOfChars[i];
}

// todo: 임시 배열에 문자열 정렬하기
for (let i = low; i <= high; i++) {
temp[frequencyOfChars[charAt(words[i], d) + 1]++] = words[i];
}

// todo: 임시 배열에 정렬한대로 원본 배열도 정렬하기
for (let i = low; i <= high; i++) {
words[i] = temp[i - low];
}

for (let i = 0; i < rangeOfChars; i++) {
sort(words, temp, low + frequencyOfChars[i], low + frequencyOfChars[i + 1] - 1, d + 1);
}
};

const sortMSD = (words) => {
Expand Down
34 changes: 33 additions & 1 deletion problem-1/sortQuickThree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,39 @@ const exchange = (words, i, j) => {
[words[i], words[j]] = [words[j], words[i]];
};

const sortQuickThree = (words, lo = 0, hi = words.length - 1, d = 0) => {
const sortQuickThree = (words, low = 0, high = words.length - 1, d = 0) => {
if (high <= low) {
return;
}

const pivot = charAt(words[low], d);

let lt = low;
let gt = high;
let i = low + 1;

while (i <= gt) {
const t = charAt(words[i], d);

if (t < pivot) { // todo: 기준 문자보다 작으면 LT 와 교환하고, LT 와 i 를 한 칸씩 이동하기
exchange(words, lt++, i++);
} else if (t > pivot) { // todo: 기준 문자보다 크다면 GT 와 교환하고, GT 를 앞으로 한 칸 이동하기
exchange(words, i, gt--);
} else {
i++; // todo: 기준 문자와 같다면 i 만 이동하기
}
}

// todo: 기준 문자보다 작은 그룹 -> 맨 처음 문자를 기준으로 다시 정렬하기
sortQuickThree(words, low, lt - 1, d);

// todo: 기준 문자와 같은 그룹 -> 다음 문자를 기준으로 다시 정렬하기
if (pivot >= 0) {
sortQuickThree(words, lt, gt, d + 1);
}

// todo: 기준 문자보다 큰 그룹 -> 맨 처음 문자를 기준으로 다시 정렬하기
sortQuickThree(words, gt + 1, high, d);
};

test('sortQuickThree는 문자열을 정렬한다.', () => {
Expand Down