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/.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..17ee6793 --- /dev/null +++ b/sorts.ts @@ -0,0 +1,73 @@ +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[] { + 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] +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..c663fed8 --- /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"] +}