From fd4f0fbd26f0f84395675e237896581534b0b989 Mon Sep 17 00:00:00 2001 From: JiHo Kim Date: Sun, 24 Dec 2023 14:22:26 +0900 Subject: [PATCH 1/7] =?UTF-8?q?=EB=B2=84=EB=B8=94=20=EC=A0=95=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-1/problem-1.test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index f54f840..6149459 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,4 +1,21 @@ const bubbleSort = (array) => { + const len = array.length; + + for (let i = 0; i < len; i++) { + let isChanged = false; + for (let j = 0; j < len - i; j++) { + if (array[j] > array[j + 1]) { + isChanged = true; + [array[j], array[j + 1]] = [array[j + 1], array[j]]; + } + } + + if (!isChanged) { + return array; + } + } + + return array; }; test.each([ From cbd7a5450220d093ee4d49b1281a58f2662cbe37 Mon Sep 17 00:00:00 2001 From: JiHo Kim Date: Sun, 24 Dec 2023 14:24:48 +0900 Subject: [PATCH 2/7] =?UTF-8?q?=EC=84=A0=ED=83=9D=20=EC=A0=95=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-2/problem-2.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 1e1fe4f..7d17ece 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -1,4 +1,17 @@ const selectionSort = (array) => { + for (let i = 0; i < array.length; i++) { + let min = i; + + for (let j = i + 1; j < array.length; j++) { + if (array[j] < array[min]) { + min = j; + } + } + + [array[i], array[min]] = [array[min], array[i]]; + } + + return array; }; test.each([ From d7e08c001495ce87a099ef37a905e32a9d7097df Mon Sep 17 00:00:00 2001 From: JiHo Kim Date: Sun, 24 Dec 2023 14:32:21 +0900 Subject: [PATCH 3/7] =?UTF-8?q?=EC=82=BD=EC=9E=85=20=EC=A0=95=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-3/problem-3.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index e4450b5..e96ea76 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -1,4 +1,12 @@ const insertionSort = (array) => { + for (let i = 1; i < array.length; i++) { + for (let j = i; j > 0; j--) { + if (array[j] < array[j - 1]) { + [array[j], array[j - 1]] = [array[j - 1], array[j]]; + } + } + } + return array; }; test.each([ From 2efb3d4f2fe2433e57d62b46e7d493413aaffcc5 Mon Sep 17 00:00:00 2001 From: JiHo Kim Date: Sun, 24 Dec 2023 15:38:44 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=EC=85=80=20=EC=A0=95=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-4/problem-4.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 069ccdf..786938b 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,4 +1,19 @@ const shellSort = (array) => { + let h = Math.floor(array.length / 2); + + while (h >= 1) { + for (let i = h; i < array.length; i++) { + for (let j = i; j >= 0; j = j - h) { + if (array[j] < array[j - h]) { + [array[j], array[j - h]] = [array[j - h], array[j]]; + } + } + } + + h = Math.floor(h / 2); + } + + return array; }; test.each([ From 45f1f18708ece40917c3a46bf0668737b879ddab Mon Sep 17 00:00:00 2001 From: JiHo Kim Date: Sun, 24 Dec 2023 16:35:18 +0900 Subject: [PATCH 5/7] =?UTF-8?q?=EB=A8=B8=EC=A7=80=20=EC=A0=95=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-5/problem-5.test.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/problem-5/problem-5.test.js b/problem-5/problem-5.test.js index 124ce9e..2542ed4 100644 --- a/problem-5/problem-5.test.js +++ b/problem-5/problem-5.test.js @@ -1,4 +1,32 @@ -const mergeSort = (array) => { +const merge = (array, start, mid, end) => { + let left = start; + let right = mid + 1; + + const temp = [...array]; + + for (let i = start; i <= end; i++) { + if (left > mid) { + array[i] = temp[right++]; + } else if (right > end) { + array[i] = temp[left++]; + } else if (temp[left] < temp[right]) { + array[i] = temp[left++]; + } else { + array[i] = temp[right++]; + } + } +}; + +const mergeSort = (array, start = 0, end = array.length - 1) => { + if (start >= end) { + return; + } + + const mid = Math.floor((start + end) / 2); + + mergeSort(array, start, mid); + mergeSort(array, mid + 1, end); + merge(array, start, mid, end); }; test.each([ From f74ceb6b8ffc362d9cb0f2a8b793e6904debc113 Mon Sep 17 00:00:00 2001 From: JiHo Kim Date: Sun, 24 Dec 2023 17:02:36 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=ED=80=B5=20=EC=A0=95=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-6/problem-6.test.js | 43 ++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index f335a1f..d1306af 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -1,4 +1,45 @@ -const quickSort = (array) => { +const partition = (array, start, end) => { + let left = start + 1; + let right = end; + + const pivot = array[start]; + + while (true) { + while (array[left] < pivot) { + if (left === end) { + break; + } + + left++; + } + + while (pivot < array[right]) { + if (right === start) { + break; + } + + right--; + } + + if (left >= right) { + break; + } + + [array[left], array[right]] = [array[right], array[left]]; + } + + [array[start], array[right]] = [array[right], array[start]]; + return right; +}; + +const quickSort = (array, start = 0, end = array.length - 1) => { + if (start >= end) { + return; + } + + const j = partition(array, start, end); + quickSort(array, start, j - 1); + quickSort(array, j + 1, end); }; test.each([ From a3c958af52d70f5af93042368b3d83453e2ea57f Mon Sep 17 00:00:00 2001 From: JiHo Kim Date: Sun, 24 Dec 2023 17:08:32 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=ED=80=B5=20=EC=A0=95=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-7/problem-7.test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problem-7/problem-7.test.js b/problem-7/problem-7.test.js index 8d36fec..900eacc 100644 --- a/problem-7/problem-7.test.js +++ b/problem-7/problem-7.test.js @@ -1,4 +1,32 @@ +const sink = (array, i, len) => { + while (2 * i <= len) { + let j = 2 * i; + + if (j < len && array[j] < array[j + 1]) { + j++; + } + + if (!(array[i] < array[j])) { + break; + } + + [array[i], array[j]] = [array[j], array[i]]; + i = j; + } +}; + const heapSort = (array) => { + let len = array.length - 1; + + for (let i = Math.floor(len / 2); i >= 1; i--) { + sink(array, i, len); + } + + while (len > 1) { + [array[1], array[len]] = [array[len], array[1]]; + len--; + sink(array, 1, len); + } }; test.each([