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 .DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.DS_Store

/generated/prisma
API
Models
mission8/
mission9/
mission6/
mission5/
mission4/
mission3/
mission2/
typescripted/
109 changes: 109 additions & 0 deletions algorithm/BinarySearchTree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
type BSTNode<T> = {
val: T;
left: BSTNode<T> | null;
right: BSTNode<T> | null;
};

class BinarySearchTree<T> {
private root: BSTNode<T> | null = null;
constructor(private compare: (a: T, b: T) => number) {}
insert(value: T): void {
if (!this.root) {
this.root = { val: value, left: null, right: null };
return;
}
let current: BSTNode<T> | null = this.root;
let parent: BSTNode<T> | null = null;
while (current !== null) {
parent = current;
if (value < current.val) {
current = current.left;
} else {
current = current.right;
}
}
if (value < parent!.val) {
parent!.left = { val: value, left: null, right: null };
} else {
parent!.right = { val: value, left: null, right: null };
}
}
find(targetValue: T) {
if (!this.root) {
return;
}
let crnt = this.root;
while (crnt) {
const comparator = this.compare(targetValue, crnt.val);
if (comparator === 0) return crnt.val;
else if (comparator < 0) crnt = crnt.left!;
else crnt = crnt.right!;
}
return null;
}
remove(target: T) {
if (!this.root) return null;

let parent: BSTNode<T> | null = null;
let crnt: BSTNode<T> | null = this.root;
let leftChild: Boolean = false;
while (crnt) {
const cmp = this.compare(target, crnt.val);
console.log(cmp);
if (cmp === 0) break;
parent = crnt;
if (cmp < 0) {
leftChild = true;
crnt = crnt.left;
} else {
leftChild = false;
crnt = crnt.right;
}
}
if (!crnt) return null;
if (!crnt.left && !crnt.right) {
if (crnt === this.root) {
this.root = null;
} else if (leftChild) {
parent!.left = null;
} else {
parent!.right = null;
}
}
if (!crnt.left || !crnt.right) {
const child = crnt.left ?? crnt.right;
this.root === crnt
? (this.root = child)
: leftChild
? (parent!.left = child)
: (parent!.right = child);
} else {
let succParent: BSTNode<T> | null = crnt;
let succ: BSTNode<T> | null = crnt.right;

while (succ && succ.left) {
succParent = succ;
succ = succ.left;
}
crnt.val = succ!.val;
while (succ) {
if (!succ.left && !succ.right) {
if (succParent.left === succ) succParent.left = null;
else succParent.right = null;
break;
} else if (succ.right) {
if (succParent!.left === succ) succParent!.left = succ.right;
else succParent!.right = succ.right;
break;
}
}
}
}
}
const tree = new BinarySearchTree<number>((a, b) => a - b);
tree.insert(8);
tree.insert(10);
tree.insert(9);
tree.insert(4);
tree.remove(4);
console.dir(tree, { depth: null });
227 changes: 227 additions & 0 deletions algorithm/sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
// merge sort
/* appraoch: divide and conquer
1. [7] | [3] | [1] | [2] | [8] | [6] | [4] | [9] | [5];

2. [3, 7] | [1, 2] | [8] | [6] | [4] | [9] | [5]

3. [1, 2, 3, 7] | [6, 8] | [4, 9] | [5]

4. [1, 2, 3, 7] | [4, 6, 8, 9] | [5]

5. [1, 2, 3, 7] | [4, 5, 6, 8, 9] => o( log n )

6. [1 ,2, 3, 4, 5, 6, 7, 8, 9] = o(n)

time complexity: o(n*logn)
space complexity: o(n)
*/

function mergeSort(nums = []) {
let n = nums.length;
if (n <= 1) return nums;
const mid = Math.floor(n / 2);

let left_nums = nums.slice(0, mid);
let right_nums = nums.slice(mid);

let sorted_left = mergeSort(left_nums);
let sorted_right = mergeSort(right_nums);

let res = [];
let idx_l = 0;
let idx_r = 0;

while (idx_l < sorted_left.length || idx_r < sorted_right.length) {
if (idx_l === sorted_left.length) {
res.push(sorted_right[idx_r]);
idx_r++;
continue;
}
if (idx_r === sorted_right.length) {
res.push(sorted_left[idx_l]);
idx_l++;
continue;
}
if (sorted_right[idx_r] <= sorted_left[idx_l]) {
res.push(sorted_right[idx_r]);
idx_r++;
} else {
res.push(sorted_left[idx_l]);
idx_l++;
}
}
return res;
}
console.log(mergeSort([7, 3, 1, 2, 8, 6, 4, 9, 5]));

/* insert
↓ : idx
⇣ : pivot


↓ ⇣
[8, 4, 7]

if nums[pivot] < nums[idx]
temp = 4
=> [8, 8, 7] shift 8
nums[pivot] = nums[idx]
=> [4, 8, 7] idx --(insert temp)

↓ ⇣
[4, 8, 7]
temp = 7
if temp < nums[idx]
=> [2, 8, 8]

=> [4, 7, 8] insert temp


time complexity => o(n ** 2)
space complexity => o(1) || O(n) => recursive

*/
function insertSort(nums) {
const n = nums.length;
for (let pIdx = 1; pIdx < n; pIdx++) {
let temp = nums[pIdx];
let idx = pIdx - 1;
while (idx >= 0 && temp < nums[idx]) {
nums[idx + 1] = nums[idx];
idx--;
}
nums[idx + 1] = temp;
}
return nums;
}

console.log(insertSort([7, 3, 1, 2, 8, 6, 4, 9, 5]));

// select sort

/*
one of the slowest sorting algorithm => on**2
1. find minimum number in the array (1 to n - 1)

2. compare nums[0: n - 1] and nums[i: n - 1] then switch it or not
↓ ⇣
[3, 5, 1, 4]
↓ ⇣
[1, 3, 5, 4]
↓ ⇣
[1, 3, 5, 4]

[1, 3, 4, 5]

=> O n**2 timecomplexity

=> O(n)

*/
function selectSort(nums) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수 명은 요구사항과 일치하도록 해주세요.

n = nums.length;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let / const 키워드를 누락해서 전역변수로 취급됩니다. 이경우는 다른 함수에 영향을 줄 수 있어서 옳지 못한 코드로 보여요. 꼭 let 혹은 const로 선언 후에 사용해주세요.

for (let i = 0; i < n; i++) {
minNum = nums[i];
minIdx = i;
for (let j = i; j < n; j++) {
if (nums[j] < minNum) {
minNum = nums[j];
minIdx = j;
}
}
[nums[i], nums[minIdx]] = [nums[minIdx], nums[i]];
}
return nums;
}
console.log(selectSort([7, 3, 1, 2, 8, 6, 4, 9, 5]));

// quick Sort
/*
pivot = 4
↓ ⇣
[7, 3, 1, 2, 4, 6, 8, 9, 5]
[][7]

↓ ⇣
[7, 3, 1, 2, 4, 6, 8, 9, 5]
[3][7]

↓ ⇣
[7, 3, 1, 2, 4, 6, 8, 9, 5]
[3, 1][7]

↓ ⇣
[7, 3, 1, 2, 4, 6, 8, 9, 5]
[3, 1, 2 ][7]

⇣ ↓
[7, 3, 1, 2, 4, 6, 8, 9, 5]
[3, 1, 2 ][7, 6]
⇣ ↓
[7, 3, 1, 2, 4, 6, 8, 9, 5]
[3, 1, 2][7, 6, 8]
⇣ ↓
[7, 3, 1, 2, 4, 6, 8, 9, 5]
[3, 1, 2][7, 6, 8, 9]
⇣ ↓
[7, 3, 1, 2, 4, 6, 8, 9, 5]
[3, 1, 2] [7, 6, 8, 9, 5]

pivot 1, 8
↓ ⇣ ↓ ⇣
[3, 1, 2] [7, 6, 8, 9, 5]
[][3] [7][]
↓ ⇣ ↓ ⇣
[3, 1, 2] [7, 6, 8, 9, 5]
[2] [3] [7, 6][]
⇣ ↓
[7, 6, 8, 9, 5]
[7, 6] [9]
⇣ ↓
[7, 6, 8, 9, 5]
[7, 6] [9]
⇣ ↓
[7, 6, 8, 9, 5]
[7, 6, 5][9]

pivot = 6
↓ ⇣
[7, 6, 5]
[][7]
⇣ ↓
[7, 6, 5]
[5] [7]

=> buttom up left + pivot + right

worst time complexity: o(n**2)
fastest time complexity : n log n

space complexity: o(n)
*/
function quickSort(nums = []) {
let n = nums.length;
let mid = Math.floor(n / 2);

let pivot = nums[mid];
let leftNums = [];
let rightNums = [];

leftIdx = 0;
rightIdx = 0;
Comment on lines +210 to +211
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

선언만 하고 사용하지 않는 변수는 제거하는 것이 좋습니다.

if (nums.length <= 1) {
return nums;
}
for (let i = 0; i < n; i++) {
if (i === mid) continue;
if (nums[i] < pivot) {
leftNums.push(nums[i]);
} else {
rightNums.push(nums[i]);
}
}
const l_sorted = quickSort(leftNums);
const r_sorted = quickSort(rightNums);
Comment on lines +223 to +224
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snake_case로 통일하거나, camelCase로 통일해주세요.

return [...l_sorted, pivot, ...r_sorted];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"해당 배열을 수정하도록" 구현이 되어있어야 하는데, 새 배열을 생성하고 있어요. nums 자체를 sort하도록 수정해보세요. (재귀)

}
console.log(quickSort([7, 3, 1, 2, 8, 6, 4, 9, 5]));
Empty file added readme.md
Empty file.