diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index f54f840..b12be9b 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,4 +1,23 @@ +const exchange = (array, a,b)=> { + [array[b], array[a]] = [array[a], array[b]]; +}; + +const less = (a,b) => a { + 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면 정렬된 배열 + } }; test.each([ diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 1e1fe4f..25957e6 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -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([ diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index e4450b5..e87c25e 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -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]], diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 069ccdf..4f1b559 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -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([ diff --git a/problem-5/problem-5.test.js b/problem-5/problem-5.test.js index 124ce9e..5668102 100644 --- a/problem-5/problem-5.test.js +++ b/problem-5/problem-5.test.js @@ -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]], diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index f335a1f..6dd2565 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -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([