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
19 changes: 19 additions & 0 deletions problem-1/problem-1.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
const exchange = (array, a,b)=> {
[array[b], array[a]] = [array[a], array[b]];
};

const less = (a,b) => a<b;

const bubbleSort = (array) => {
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면 정렬된 배열
Comment on lines +12 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

한 for문을 실행할 때 단 한 번도 교환이 안됐다면, 정렬이 되었다는 것을 확신할 수 있어요. else로 해버리면 마지막에만 교환이 안되어도 모두 정렬되었다고 판단해버릴 수 있어요.

    for (let j = 0; j< (length -i -1); j++) {
      let isAligned = true;
      
      if (less(array[j + 1], array[j])) {
        exchange (array, j, j+1);
        isAligned = false;
      } 
    }
    
    if (isAligned) {
      return array;
    }

}
};

test.each([
Expand Down
22 changes: 22 additions & 0 deletions problem-2/problem-2.test.js
Original file line number Diff line number Diff line change
@@ -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([
Expand Down
18 changes: 18 additions & 0 deletions problem-3/problem-3.test.js
Original file line number Diff line number Diff line change
@@ -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]],
Expand Down
25 changes: 25 additions & 0 deletions problem-4/problem-4.test.js
Original file line number Diff line number Diff line change
@@ -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([
Expand Down
37 changes: 36 additions & 1 deletion problem-5/problem-5.test.js
Original file line number Diff line number Diff line change
@@ -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]],
Expand Down
61 changes: 61 additions & 0 deletions problem-6/problem-6.test.js
Original file line number Diff line number Diff line change
@@ -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([
Expand Down