From 1ec7af64664350e2b9af4f3e0164aed809a79c8a Mon Sep 17 00:00:00 2001 From: gy Date: Wed, 13 May 2026 11:09:46 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EC=95=8C=EA=B3=A0=EB=A6=AC?= =?UTF-8?q?=EC=A6=98=20=ED=8C=8C=EC=9D=BC=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 10 ++++++++++ package-lock.json | 29 +++++++++++++++++++++++++++++ package.json | 17 +++++++++++++++++ sorts.ts | 40 ++++++++++++++++++++++++++++++++++++++++ test.ts | 32 ++++++++++++++++++++++++++++++++ tsconfig.json | 24 ++++++++++++++++++++++++ 6 files changed, 152 insertions(+) create mode 100644 .gitignore create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 sorts.ts create mode 100644 test.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..9764a528 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# Dependency directories +node_modules/ + +# IDE settings +.idea/ +.vscode/ + +# Build outputs +dist/ +out/ diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..f69bcdc4 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,29 @@ +{ + "name": "algorithm", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "algorithm", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "typescript": "^6.0.3" + } + }, + "node_modules/typescript": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.3.tgz", + "integrity": "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==", + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 00000000..ff8531bc --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "algorithm", + "version": "1.0.0", + "description": "", + "license": "ISC", + "author": "", + "type": "module", + "main": "app.js", + "scripts": { + "build": "tsc", + "start": "npm run start", + "dev": "npm run dev" + }, + "dependencies": { + "typescript": "^6.0.3" + } +} diff --git a/sorts.ts b/sorts.ts new file mode 100644 index 00000000..aa90ae89 --- /dev/null +++ b/sorts.ts @@ -0,0 +1,40 @@ +function selectionSort(arr: number[]): number[] { + for (let i = 0; i < arr.length; i++) { + let minIndex = i; + for (let j = i + 1; j < arr.length; j++) { + if (arr[j] < arr[minIndex]) { + minIndex = j; + } + } + if (minIndex !== i) { + [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]; + } + } + return arr; +} + +function insertionSort(arr: number[]): number[] { + for (let i = 1; i < arr.length; i++) { + const currentValue = arr[i]; + let j = i - 1; + while(j >= 0 && arr[j] > currentValue) { + arr[j + 1] = arr[j]; + j--; + } + arr[j+1] = currentValue; + } + return arr; +} + +function mergeSort(arr: number[]): number[] { + +} + +function quickSort(arr: number[]): number[] { + +} + +console.log(selectionSort([64, 25, 12, 22, 11])); // [11, 12, 22, 25, 64] +console.log(insertionSort([12, 11, 13, 5, 6])); // [5, 6, 11, 12, 13] +console.log(mergeSort([12, 11, 13, 5, 6])); // [5, 6, 11, 12, 13] +console.log(quickSort([12, 11, 13, 5, 6])); // [5, 6, 11, 12, 13] \ No newline at end of file diff --git a/test.ts b/test.ts new file mode 100644 index 00000000..b8441f43 --- /dev/null +++ b/test.ts @@ -0,0 +1,32 @@ +function selectionSort(arr: number[]): number[] { + for (let index = 0; index < arr.length; index++) { + let minIndex = index; + for (let j = index + 1; j < arr.length; j++) { + if (arr[j] < arr[minIndex]) { + minIndex = j; + } + } + + if (minIndex !== index) { + [arr[index], arr[minIndex]] = [arr[minIndex], arr[index]]; + } + } + return arr; +} + +function insertionSort(arr: number[]): number[] { + +} + +function mergeSort(arr: number[]): number[] { + +} + +function quickSort(arr: number[]): number[] { + +} + +console.log(selectionSort([64, 25, 12, 22, 11])); // [11, 12, 22, 25, 64] +console.log(insertionSort([12, 11, 13, 5, 6])); // [5, 6, 11, 12, 13] +//console.log(mergeSort([12, 11, 13, 5, 6])); // [5, 6, 11, 12, 13] +//console.log(quickSort([12, 11, 13, 5, 6])); // [5, 6, 11, 12, 13] \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..eb5dc33b --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,24 @@ +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + "module": "nodenext", + "target": "ESNext", + "types": [], + "sourceMap": true, + "declaration": true, + "declarationMap": true, + "noUncheckedIndexedAccess": false, + "exactOptionalPropertyTypes": true, + "strict": true, + "skipLibCheck": true, + "esModuleInterop": true, + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + + + }, + "include": ["src/**/*"], + "exclude": ["node_modules"] +} From 2e54767338137dab75687985523da99abe1bc7e5 Mon Sep 17 00:00:00 2001 From: codes-gy Date: Thu, 14 May 2026 10:44:55 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=EC=A0=95=EB=A0=AC=20=EC=95=8C?= =?UTF-8?q?=EA=B3=A0=EB=A6=AC=EC=A6=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/PULL_REQUEST_TEMPLATE.md | 54 +++++++++++++++++++------------- sorts.ts | 35 ++++++++++++++++++++- test.ts | 32 ------------------- tsconfig.json | 48 ++++++++++++++-------------- 4 files changed, 91 insertions(+), 78 deletions(-) delete mode 100644 test.ts diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index ec85f6f1..cec1a663 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,21 +1,33 @@ - -## 요구사항 - -### 기본 -- [x] 기본 항목 1 -- [ ] 기본 항목 2 - -### 심화 -- [ ] 심화 항목 1 -- [ ] 심화 항목 2 - -## 주요 변경사항 -- -- - -## 스크린샷 -![image](이미지url) - -## 멘토에게 -- 셀프 코드 리뷰를 통해 질문 이어가겠습니다. -- + +## 요구사항 +- 자바스크립트로 정렬 알고리즘 구현하기 + +### 기본 +- [x] **선택 정렬 (Selection sort)** + - 숫자형 배열을 파라미터로 받고, 해당 배열을 수정하도록 구현합니다. +- [x] **삽입 정렬 (Insertion sort)** + - 숫자형 배열을 파라미터로 받고, 해당 배열을 수정하도록 구현합니다. +- [x] **퀵 정렬 (Quick sort)** + - 숫자형 배열을 파라미터로 받고, 해당 배열을 수정하도록 구현합니다. +- [x] **병합 정렬 (Merge sort)** + - 숫자형 배열을 파라미터로 받고, 해당 배열을 수정하도록 구현합니다. + +## 함수 예시 +- 해당 배열을 직접 수정하는 예시 +```javascript +const nums = [3, 1, 2]; +console.log(nums); // [3, 1, 2]; +selectionSort(nums); +console.log(nums); // [1, 2, 3] +``` + +## 주요 변경사항 +- +- + +## 스크린샷 +![image](이미지url) + +## 멘토에게 +- 셀프 코드 리뷰를 통해 질문 이어가겠습니다. +- diff --git a/sorts.ts b/sorts.ts index aa90ae89..17ee6793 100644 --- a/sorts.ts +++ b/sorts.ts @@ -27,11 +27,44 @@ function insertionSort(arr: number[]): number[] { } function mergeSort(arr: number[]): number[] { + if (arr.length <= 1) return arr; -} + const mid = Math.floor(arr.length / 2); + const left = arr.slice(0, mid); + const right = arr.slice(mid); + return merge(mergeSort(left), mergeSort(right)); +} +function merge(left : number[], right: number[]) : number[] { + const result : number[] = []; + let i = 0; + let j = 0; + while (i < left.length && j < right.length) { + if (left[i] <= right[j]) { + result.push(left[i]); + i++; + } else { + result.push(right[j]); + j++; + } + } + return [...result, ...left.slice(i), ...right.slice(j)]; +} function quickSort(arr: number[]): number[] { + if (arr.length <= 1) return arr; + + const pivot = arr[arr.length - 1]; + const left : number[] = []; + const right: number[] = []; + for (let i = 0; i < arr.length - 1; i++) { + if (arr[i] < pivot) { + left.push(arr[i]); + } else { + right.push(arr[i]); + } + } + return [...quickSort(left), pivot, ...quickSort(right)]; } console.log(selectionSort([64, 25, 12, 22, 11])); // [11, 12, 22, 25, 64] diff --git a/test.ts b/test.ts deleted file mode 100644 index b8441f43..00000000 --- a/test.ts +++ /dev/null @@ -1,32 +0,0 @@ -function selectionSort(arr: number[]): number[] { - for (let index = 0; index < arr.length; index++) { - let minIndex = index; - for (let j = index + 1; j < arr.length; j++) { - if (arr[j] < arr[minIndex]) { - minIndex = j; - } - } - - if (minIndex !== index) { - [arr[index], arr[minIndex]] = [arr[minIndex], arr[index]]; - } - } - return arr; -} - -function insertionSort(arr: number[]): number[] { - -} - -function mergeSort(arr: number[]): number[] { - -} - -function quickSort(arr: number[]): number[] { - -} - -console.log(selectionSort([64, 25, 12, 22, 11])); // [11, 12, 22, 25, 64] -console.log(insertionSort([12, 11, 13, 5, 6])); // [5, 6, 11, 12, 13] -//console.log(mergeSort([12, 11, 13, 5, 6])); // [5, 6, 11, 12, 13] -//console.log(quickSort([12, 11, 13, 5, 6])); // [5, 6, 11, 12, 13] \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index eb5dc33b..c663fed8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,24 +1,24 @@ -{ - // Visit https://aka.ms/tsconfig to read more about this file - "compilerOptions": { - "module": "nodenext", - "target": "ESNext", - "types": [], - "sourceMap": true, - "declaration": true, - "declarationMap": true, - "noUncheckedIndexedAccess": false, - "exactOptionalPropertyTypes": true, - "strict": true, - "skipLibCheck": true, - "esModuleInterop": true, - "verbatimModuleSyntax": true, - "isolatedModules": true, - "noUncheckedSideEffectImports": true, - "moduleDetection": "force", - - - }, - "include": ["src/**/*"], - "exclude": ["node_modules"] -} +{ + // Visit https://aka.ms/tsconfig to read more about this file + "compilerOptions": { + "module": "nodenext", + "target": "ESNext", + "types": [], + "sourceMap": true, + "declaration": true, + "declarationMap": true, + "noUncheckedIndexedAccess": false, + "exactOptionalPropertyTypes": true, + "strict": true, + "skipLibCheck": true, + "esModuleInterop": true, + "verbatimModuleSyntax": true, + "isolatedModules": true, + "noUncheckedSideEffectImports": true, + "moduleDetection": "force", + + + }, + "include": ["src/**/*"], + "exclude": ["node_modules"] +}