diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index f54f840..d46fe48 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,4 +1,21 @@ const bubbleSort = (array) => { + for(let i = 0; i <= array.length; i++){ + let swapped = false; + + for(let j = 0; j < array.length - i; j ++){ + if(array[j] > array[j + 1]){ + const temp = array[j]; + + array[j] = array[j + 1]; + array[j + 1] = temp; + swapped = true; + } + } + + if(!swapped) break; + } + + return array; }; test.each([ diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 1e1fe4f..80bf12f 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -1,5 +1,30 @@ +const exchange = (array, i, j) => { + [array[i], array[j]] = [array[j], array[i]]; +} + +const findMinIndex = (array, startIndex) => { + let minIndex = startIndex; + + for(let i = startIndex; i < array.length; i++){ + if(array[minIndex] > array[i]){ + minIndex = i; + } + } + + return minIndex; +} + const selectionSort = (array) => { -}; + for(let i = 0; i <= array.length; i++){ + const minIndex = findMinIndex(array, i); + + if(minIndex === i ){ + continue; + } + + exchange(array, i , minIndex); + } +} test.each([ [[5, 4, 3, 2, 1]], diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index e4450b5..ba5c877 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -1,4 +1,15 @@ +const exchange = (array, a, b) => { + [array[b], array[a]] = [array[a], array[b]]; +} + const insertionSort = (array) => { + for(let i = 1; i < array.length; i++) { + let j = i; + while(j > 0 && array[j - 1] > array[j]) { + exchange(array, j, j - 1); + j--; + } + } }; test.each([ diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 069ccdf..83c179a 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,4 +1,27 @@ +const exchange = (array, j, i) => { + [array[j], array[i]] = [array[i], array[j]]; +} + const shellSort = (array) => { + let gap = Math.floor(array.length / 2); + + while(gap > 0){ + for(let i = gap; i < array.length; i++){ + for(let j = i; j >= 0; j = j - gap){ + + if(array[j] < array[j - gap]){ + exchange(array, j, j - gap); + }else { + break; + } + + } + } + + gap = Math.floor(gap / 2); + } + + }; test.each([ diff --git a/problem-5/problem-5.test.js b/problem-5/problem-5.test.js index 124ce9e..a67204f 100644 --- a/problem-5/problem-5.test.js +++ b/problem-5/problem-5.test.js @@ -1,4 +1,34 @@ -const mergeSort = (array) => { +const merge = (array, start, mid, end) => { + let left = start, + right = mid + 1; + + const copyArray = [...array]; + + for(let i = start; i <= end; i++){ + if(left > mid){ // 오른쪽 요소로 이동 + array[i] = copyArray[right]; + right++; + }else if(right > end){ // 왼쪽 요소로 이동 + array[i] = copyArray[left]; + left++; + }else if(copyArray[left] < copyArray[right]){ // 왼쪽과 오른쪽 요소 중 왼쪽 값이 더 작을 경우 + array[i] = copyArray[left]; + left++; + }else{ + array[i] = copyArray[right]; // 왼쪽과 오른쪽 요소 중 오른쪽 값이 더 작을 경우.. + 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([ diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index f335a1f..9a91ee2 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -1,4 +1,60 @@ + +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 exchange = (array, low, high) => { + [array[high], array[low]] = [array[low], array[high]]; +} + +const less = (a, b) => a < b; + +const patition = (array, low, high) => { + let left = low + 1, + right = high; + + const pivot = array[low]; + + while(true){ + while(less(array[left], pivot)){ + if(left === high) break; + + left++; + } + + while(less(pivot, array[right])){ + if(right === low) break; + + right--; + } + + if(left >= right) break; // 가리키는 요소가 겹칠 때 + exchange(array, left, right); + } + + exchange(array, low, right); + return right; +} + +const sort = (array, low, high) => { + if(low >= high) return; + + const index = patition(array, low, high); + sort(array, low, index - 1); + sort(array, index + 1, high); +} + const quickSort = (array) => { + shuffle(array); // 배열 무작위로 섞음 + sort(array, 0, array.length - 1); }; test.each([ diff --git a/problem-7/problem-7.test.js b/problem-7/problem-7.test.js index 8d36fec..8c86a9b 100644 --- a/problem-7/problem-7.test.js +++ b/problem-7/problem-7.test.js @@ -1,4 +1,34 @@ +const exchange = (array, a, b) => { + [array[a], array[b]] = [array[b], array[a]]; +} + +const less = (a, b) => a < b; + +const sink = (array, i, N) => { + while(2 * i <= N){ + let j = 2 * i; + if(j < N && less(array[j], array[j + 1])){ + j++; + } + + if(!less(array[i], array[j])) break; + + exchange(array, i, j); + i = j; + } +} const heapSort = (array) => { + let N = array.length - 1; + + for(let i = Math.floor(N / 2); i >= 1; i--){ + sink(array, i, N); + } + + while(N > 1){ + exchange(array, 1, N); + N--; + sink(array, 1, N); + } }; test.each([