Skip to content

3주차 과제 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
17 changes: 17 additions & 0 deletions problem-1/problem-1.test.js
Original file line number Diff line number Diff line change
@@ -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([
Expand Down
13 changes: 13 additions & 0 deletions problem-2/problem-2.test.js
Original file line number Diff line number Diff line change
@@ -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([
Expand Down
8 changes: 8 additions & 0 deletions problem-3/problem-3.test.js
Original file line number Diff line number Diff line change
@@ -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([
Expand Down
15 changes: 15 additions & 0 deletions problem-4/problem-4.test.js
Original file line number Diff line number Diff line change
@@ -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([
Expand Down
30 changes: 29 additions & 1 deletion problem-5/problem-5.test.js
Original file line number Diff line number Diff line change
@@ -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([
Expand Down
43 changes: 42 additions & 1 deletion problem-6/problem-6.test.js
Original file line number Diff line number Diff line change
@@ -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([
Expand Down
28 changes: 28 additions & 0 deletions problem-7/problem-7.test.js
Original file line number Diff line number Diff line change
@@ -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([
Expand Down