From cb939cbd0e681029dc494f363898f8de66908a0a Mon Sep 17 00:00:00 2001 From: jinvicky Date: Mon, 18 Sep 2023 15:25:45 +0900 Subject: [PATCH 1/6] =?UTF-8?q?Feat:=20=EB=B2=84=EB=B8=94=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC=20=EA=B3=BC=EC=A0=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-1/problem-1.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index f54f840..b12be9b 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,4 +1,23 @@ +const exchange = (array, a,b)=> { + [array[b], array[a]] = [array[a], array[b]]; +}; + +const less = (a,b) => a { + const {length} = array; + let isAligned = false; + + for (let i = 0; i< length; i++) { + for (let j = 0; j< (length -i -1); j++) { + if (less(array[j + 1], array[j])) { + exchange (array, j, j+1); + isAligned = false; + } + else isAligned = true; // exchange하지 않았을 경우 정렬된 상태 + } + if (isAligned) return array; // j를 한 바퀴 돌 동안 isAligned가 true면 정렬된 배열 + } }; test.each([ From 53320587bfec6c170709aa5f2fc62c3b4f9b851b Mon Sep 17 00:00:00 2001 From: jinvicky Date: Mon, 18 Sep 2023 16:20:04 +0900 Subject: [PATCH 2/6] =?UTF-8?q?Feat:=20=20=EC=84=A0=ED=83=9D=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-2/problem-2.test.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 1e1fe4f..25957e6 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -1,4 +1,26 @@ +const exchange = (list, a,b) => { + [list[b], list[a]] = [list[a], list[b]]; +}; + +const less = (a, b) => a < b; + +const findMinIndex = (list, startIndex) => { + let min = startIndex; + + for (let j = startIndex + 1; j < list.length; j++) { + if (less(list[j], list[min])) { + min = j; + } + } + return min; +} + const selectionSort = (array) => { + for (let i = 0; i < array.length - 1; i++) { + const minIndex = findMinIndex(array, i); + + exchange(array, i, minIndex); + } }; test.each([ From 576edd62e8d6f478d55acfe8f7b8f2c88ca35735 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Mon, 18 Sep 2023 17:32:55 +0900 Subject: [PATCH 3/6] =?UTF-8?q?Feat:=20=20=EC=82=BD=EC=9E=85=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-3/problem-3.test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index e4450b5..e87c25e 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -1,6 +1,24 @@ +const exchange = (list, a,b) => { + [list[b], list[a]] = [list[a], list[b]]; +}; + +const less = (a,b) => a < b; + const insertionSort = (array) => { + const {length} = array; + + for (let i = 1; i < length; i++) { + for (let j = i; j > 0; j--) { + if (less(array[j], array[j - 1])) { + exchange(array, j, j - 1); + } else { + break; + } + } + } }; + test.each([ [[5, 4, 3, 2, 1]], [[1, 2, 3, 4, 5]], From 685d6f5db3e6263f4a510f206c646c3582e5c88c Mon Sep 17 00:00:00 2001 From: jinvicky Date: Mon, 18 Sep 2023 17:43:24 +0900 Subject: [PATCH 4/6] =?UTF-8?q?Feat:=20=EC=85=B8=20=EC=A0=95=EB=A0=AC=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-4/problem-4.test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 069ccdf..4f1b559 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,4 +1,29 @@ +const exchange = (list, a,b) => { + [list[b], list[a]] = [list[a], list[b]]; +}; + +const less = (a,b) => a < b; + const shellSort = (array) => { + const { length } = array; + + let h = 1; + while (h < (length / 3)) { + h = 3 * h + 1; + } + + while (h >= 1) { + for (let i = h; i < length; i++) { + for (let j = i; j >= 0; j = j - h) { + if (less(array[j], array[j - h])) { + exchange(array, j, j - h); + } else { + break; + } + } + } + h = Math.floor(h / 3); + } }; test.each([ From e8b18c69207fab05e0e3e5cc376fdbcadae06ac8 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Mon, 18 Sep 2023 17:51:25 +0900 Subject: [PATCH 5/6] =?UTF-8?q?Feat:=20=EB=A8=B8=EC=A7=80=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-5/problem-5.test.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/problem-5/problem-5.test.js b/problem-5/problem-5.test.js index 124ce9e..5668102 100644 --- a/problem-5/problem-5.test.js +++ b/problem-5/problem-5.test.js @@ -1,6 +1,41 @@ -const mergeSort = (array) => { +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; + + const temp = [...list]; + + for (let i = start; i <= end; i++) { + if (left > mid) { + list[i] = temp[right]; + right++; + } else if (right > end) { + list[i] = temp[left]; + left++; + } else if (less(temp[left], temp[right])) { + list[i] = temp[left]; + left++; + } else { + list[i] = temp[right]; + right++; + } + } +} + + + test.each([ [[8, 7, 6, 5, 4, 3, 2, 1]], [[1, 2, 3, 4, 5, 6, 7, 8]], From f7963b9d29df69f664eaef9c42c51dba617b9699 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Mon, 18 Sep 2023 18:04:16 +0900 Subject: [PATCH 6/6] =?UTF-8?q?Feat:=20=ED=80=B5=20=EC=A0=95=EB=A0=AC=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-6/problem-6.test.js | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index f335a1f..6dd2565 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -1,4 +1,65 @@ +const exchange = (list, a, b) => { + [list[b], list[a]] = [list[a], list[b]]; +}; + +const less = (a, b) => a < b; + +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, lo, hi) => { + let left = lo + 1; + let right = hi; + + const pivot = array[lo]; + + while(true) { + while (less(array[left], pivot)) { + if(left === hi) { + break; + } + + left++; + } + + while (less(pivot, array[right])) { + if (right === lo) { + break; + } + right--; + } + + if (left >= right) { + break; + } + exchange(array, left, right); + } + + exchange(array, lo, right); + return right; +}; + +const sort = (array, lo, hi) => { + if (lo >= hi) { + return; + } + + const j = partition(array, lo, hi); + sort(array, lo, j - 1); + sort(array, j + 1, hi); +}; + const quickSort = (array) => { + shuffle(array); + sort(array, 0, array.length - 1); }; test.each([